为什么AbstractCollection没有实现equals()?

Mic*_*hel 7 java collections hashmap

你知道吗 :

Map<Object,Object> m1 = new HashMap<Object, Object>();
Map<Object,Object> m2 = new HashMap<Object, Object>();
System.out.println("m1.equals(m2) = "+m1.equals(m2));
System.out.println("m1.keySet().equals(m2.keySet()) = "
            +m1.keySet().equals(m2.keySet()));
System.out.println("m1.entrySet().equals(m2.entrySet()) = "
            +m1.entrySet().equals(m2.entrySet()));
System.out.println("m1.values().equals(m2.values()) = "
            +m1.values().equals(m2.values()));
Run Code Online (Sandbox Code Playgroud)

输出:

m1.equals(m2) = true
m1.keySet().equals(m2.keySet()) = true
m1.entrySet().equals(m2.entrySet()) = true
m1.values().equals(m2.values()) = false
Run Code Online (Sandbox Code Playgroud)

这是因为AbstractCollection(HashMap$Values继承自)不会覆盖的事实#equals().

你知道为什么会这样吗?

Gre*_*ase 6

根据合同Collection#equals(),Collections 没有通用的equals()方法,因此AbstractCollection不能提供一个.

请注意,HashMap$Values既不是Set也不是List,因此是窘境,从某种意义上说它是不支持的原因equals().