成都精品网站建设,seo站长综合查询工具,广州专业做网站排名哪家好,请人做网站当一个类有可能会和其他类发生比较的时候#xff0c;我们会重写equals方法#xff0c;但大多数情况下#xff0c;都忽略了重写hashCode方法。 这里说一下重写hashCode的必要性。 当我们使用HashSet或者HashMap的时候#xff0c;在比对value|key是否存在时#xff0c;会调用…当一个类有可能会和其他类发生比较的时候我们会重写equals方法但大多数情况下都忽略了重写hashCode方法。 这里说一下重写hashCode的必要性。 当我们使用HashSet或者HashMap的时候在比对value|key是否存在时会调用hashCode方法。 注意hashSet的contains方法其实是依赖于HashMap的containsKey方法的。 我们来看下containsKey方法的实现 public boolean containsKey(java.lang.Object paramObject){return (getEntry(paramObject) ! null);}final EntryK, V getEntry(java.lang.Object paramObject){int i (paramObject null) ? 0 : hash(paramObject.hashCode());Entry localEntry this.table[indexFor(i, this.table.length)];for (; localEntry ! null; localEntry localEntry.next){if (localEntry.hash i) { java.lang.Object localObject;if (((localObject localEntry.key) paramObject) || ((paramObject ! null) (paramObject.equals(localObject)))){return localEntry; } }}return null;} 由上面代码即可知hashCode是重要的判断依据没有重写hashCodeequals表现相等的两个类它们的hashCode并不相等。 所以会导致containsKey方法返回false测试代码如下 包含HashCode的类 package hashset.and.hashcode;public class ClassWithHashCode {public int i;public boolean equals(Object o) {if (o this)return true;if (o instanceof ClassWithHashCode) {ClassWithHashCode code (ClassWithHashCode) o;return code.i i;}return false;}public int hashCode() {return i * 17 37;}
} 没有重写hasCode的类 package hashset.and.hashcode;public class ClassWithoutHashCode {public int i;public boolean equals(Object o) {if (o this)return true;if (o instanceof ClassWithoutHashCode) {ClassWithoutHashCode code (ClassWithoutHashCode) o;return code.i i;}return false;}
} 测试类 package hashset.and.hashcode;import java.util.HashSet;public class Test {/*** param args*/public static void main(String[] args) {ClassWithHashCode c1 new ClassWithHashCode();ClassWithHashCode c2 new ClassWithHashCode();c1.i 0;c2.i 0;HashSetClassWithHashCode set new HashSetClassWithHashCode();set.add(c1);System.out.println(set.contains(c2));ClassWithoutHashCode co1 new ClassWithoutHashCode();ClassWithoutHashCode co2 new ClassWithoutHashCode();co1.i 0;co2.i 0;HashSetClassWithoutHashCode set1 new HashSetClassWithoutHashCode();set1.add(co1);System.out.println(set.contains(co2));}
} 执行的结果为 truefalse 符合预期。证毕。转载于:https://www.cnblogs.com/anrainie/archive/2012/04/27/2473196.html