Java で HashSet の add

というか

    public boolean add(E e) {
	return map.put(e, PRESENT)==null;
    }

なので HashMap の挙動なんだけど

    public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

な感じ HashCode の比較と equals が || で評価されてて
ちょっと気になったので書いてみた。
(基本、map の put なども含めて HashCode の一致を見て真だったら、equals使うと思ってたので)

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		class A {

			@Override
			public boolean equals(Object obj) {
				return true;
			}
			
		}
		class B {
			@Override
			public boolean equals(Object obj) {
				return true;
			}

			@Override
			public int hashCode() {
				return 1;
			}
			
		}
		
		HashSet<A> hashSet = new HashSet<A>();
		hashSet.add(new A());
		hashSet.add(new A());
		hashSet.add(new A());
		hashSet.add(new A());
		hashSet.add(new A());
		HashSet<B> hashSet2 = new HashSet<B>();
		hashSet2.add(new B());
		hashSet2.add(new B());
		hashSet2.add(new B());
		hashSet2.add(new B());
		hashSet2.add(new B());
		System.out.println("A's hashset : " + hashSet.size());
		System.out.println("B's hashset : " + hashSet2.size());
	}

}

結果

A's hashset : 5
B's hashset : 1

あってた