Evaluating objects containing strings with Java ArrayList contains ()

I would like to do a deeper check of String objects to be able to do the following:

List<MyObj> myList = new ArrayList<MyObj>() {{
    add(new MyObj("hello"));
    add(new MyObj("world"));
}};

System.out.println(myList.contains("hello")); // true
System.out.println(myList.contains("foo")); // false
System.out.println(myList.contains("world")); // true

But getting false data on each of them with the following full code

/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
    public static class MyObj {
        private String str;
        private int hashCode;

        public MyObj(String str) {
            this.str = str;
        }

        public @Override boolean equals(Object obj) {
            System.out.println("MyObj.equals(Object)");
            if (obj == this) {
                return true;
            }

            if (obj instanceof String) {
                String strObj = (String) obj;
                return strObj.equals(str);
            }

            return false;
        }

        public @Override int hashCode() {
            if (hashCode == 0) {
                hashCode = 7;
                for (int i = 0; i < str.length(); ++i) {
                    hashCode = hashCode * 31 + str.charAt(i);
                }
            }

            return hashCode;
        }
    }

    public static final MyObj obj1 = new MyObj("hello");
    public static final MyObj obj2 = new MyObj("world");
    public static void main (String[] args) throws java.lang.Exception {
        List<MyObj> myList = new ArrayList<MyObj>() {{
            add(obj1);
            add(obj2);
        }};

        System.out.println(myList.contains("hello"));
        System.out.println(myList.contains("foo"));
        System.out.println(myList.contains("world"));
    }
}

If I'm right, List object must be used equals()and hashCode()to assess the contained objects. So I @Overridechecked them and String them additionally. But he never gets in equals(), because there is no way out MyObj.equals(Object).

+4
source share
6 answers

java.util.ArrayList#indexOfused inside ArrayList for contains().

There is a check

o.equals(elementData[i])

So, there is a string comparison with your object, so it String.equals()is called to check for equality.

+6

equals:

equals :

  • : , x.equals() . .
  • : x y x.equals(y) true , y.equals(x) true. .
  • : x, y z, x.equals(y) true, y.equals(z) true, x.equals(z) true,
  • : x y x.equals(y) true false, , , .
  • , x.equals(NULL) .

, , .

, , , equals , ArrayList, , "hello".equals(new MyObj("hello")). , equals , .

+5

, , equals . myList.contains("hello"), ArrayList , "hello".equals(<MyObj>), .

equals , MyObject value , , :

public boolean myContains(List<MyObj> list, String value) {
    return list.contains(new MyObj(value));
}
+1
List<MyObj> myList = new ArrayList<MyObj>()

- MyObj, MyObj myList.contains:

System.out.println(myList.contains(new MyObj("hello")));
System.out.println(myList.contains(new MyObj("foo")));
System.out.println(myList.contains(new MyObj("world")));
0

String MyObj... "". :

Map<String, MyObj> map = new HashMap<String, MyObj>() {{
    put("hello", new MyObj("hello"));
    put("world", new MyObj("world"));
}};

:

if (map.containsKey("hello"))

:

MyObj o = map.get("hello"); // get() returns null if key not found
0

This is not equal to your object, which is called when contains contains, but corresponds to the String class. And the String implementation checks with the instance whether the class is a string to perform string operations to determine the answer. If the object is not a string, it returns false;

0
source

All Articles