使用Java ArrayList评估包含字符串的对象包含()

dtr*_*unk 4 java arraylist

我想对Objects进行更深入的String检查,以便能够执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)

但是使用以下完整代码在每个上面都会出错

/* 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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我是对的,列表对象应该使用equals()hashCode()评估包含对象.所以我@Override他们另外检查他们的字符串.但它永远不会进入,equals()因为没有输出MyObj.equals(Object).

Adm*_*mit 7

java.util.ArrayList#indexOf在ArrayList内部使用contains().

有一张支票,

o.equals(elementData[i])
Run Code Online (Sandbox Code Playgroud)

因此,字符串与您的对象进行比较,因此String.equals()调用以检查相等性.


Jac*_*ack 5

你根本没有履行equals合同:

equals方法在非null对象引用上实现等价关系:

  • 它是自反的:对于任何非空引用值x,x.equals(x)应该返回true.你的不是反身.
  • 它是对称的:对于任何非空引用值x和y,当且仅当y.equals(x)返回true时,x.equals(y)才应返回true.你的不对称.
  • 它是传递性的:对于任何非空引用值x,y和z,如果x.equals(y)返回true而y.equals(z)返回true,则x.equals(z)应返回true.你的不是传递性的
  • 它是一致的:对于任何非空引用值x和y,x.equals(y)的多次调用始终返回true或始终返回false,前提是不修改在对象的equals比较中使用的信息.
  • 对于任何非空引用值x,x.equals(null)应返回false.

因此,如果不尊重方法的合同,您就不能指望预期的行为.

例如,什么保证你将在ArrayList中包含的对象上调用equals,而不是以其他方式调用,例如"hello".equals(new MyObj("hello")).你不能保证它,但因为equals通常应该是对称的而不是你不应该介意的.