ArrayList not using the overridden equals

后端 未结 11 1400
误落风尘
误落风尘 2021-02-08 14:13

I\'m having a problem with getting an ArrayList to correctly use an overriden equals. the problem is that I\'m trying to use the equals to only test for a single key field, and

11条回答
  •  梦毁少年i
    2021-02-08 14:54

    There are two errors in your code.

    First: The "contains" method called on "objectList" object should pass a new InnerClass object as the parameter.

    Second: The equals method (should accept the parameter as Object, and is correct) should handle the code properly according to the received object. Like this:

    @Override
        public boolean equals (Object in) {
            System.out.println("reached here");
            if(in == null) {
            return false;
            }else if( in instanceof InnerClass) {
            String inString = ((InnerClass)in).testKey;
            return testKey == null ? false : testKey.equals(inString);
            }else {
            return false;
            }       
        }  
    

提交回复
热议问题