62,615
社区成员
发帖
与我相关
我的任务
分享
HashSet<int[]> set = new HashSet<int[]>();
int[] whole= {1,2};
set.add(whole);
int[] xwhole= {1,2};
set.add(xwhole);
Iterator<int[]> ite = set.iterator();
while (ite.hasNext()) {
System.out.println(Arrays.toString(ite.next()));
}
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;
}
public static void main(String[] args){
HashSet<int[]> set = new HashSet<int[]>();
int[] whole= {1,2};
set.add(whole);
int[] xwhole= {1,2};
set.add(xwhole);
Iterator<int[]> ite = set.iterator();
while (ite.hasNext()) {
System.out.println(Arrays.toString(ite.next()));
}
System.out.println(whole.equals(xwhole));
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
HashSet<int[]> set = new HashSet<int[]>();
int[] whole= {1,2};
set.add(whole);
int[] xwhole= {1,2};
set.add(xwhole);
Iterator<int[]> ite = set.iterator();
while (ite.hasNext()) {
System.out.println(Arrays.toString(ite.next()));
}
System.out.println(whole);
System.out.println(xwhole);
System.out.println(Arrays.equals(whole, xwhole));
}
}
System.out.println(whole.hashCode());
System.out.println(xwhole.hashCode());
System.out.println(whole.equals(xwhole));
System.out.println(Arrays.toString(whole.getClass().getInterfaces()));
6727959
31929496
false
[interface java.lang.Cloneable, interface java.io.Serializable]
System.out.println(whole);
System.out.println(xwhole);