草庐IT

hashCodes

全部标签

java - 尽管同时实现了 hashCode() 和 equals(),但 HashSet 添加了重复条目

我有以下类(class):classPoint{doublex,y;//....constructorandotherfunctionsherepublicbooleanequals(Pointp){if(p==null)return(false);return(x==p.x&&y==p.y);}publicinthashCode(){intresult=17;longc1=Double.doubleToLongBits(x);longc2=Double.doubleToLongBits(y);intci1=(int)(c1^(c1>>>32));intci2=(int)(c2^(c2

java - 使用 Mojo Jaxb2 maven 插件创建 Java 类时生成 hashCode() 和 equals()

我正在处理的代码使用org.codehaus.mojo中的jaxb2-maven-plugin从XSD模式生成Java类。我正在寻找一种方法来为这些类自动实现equals()和hashCode()方法,但似乎没有办法。我知道还有其他JAXB2Maven插件可以做到这一点(例如http://confluence.highsource.org/display/J2B/Home),但我想知道你们之前是否遇到过这个问题,是否有办法修复它。我正在使用xjc目标生成类。 最佳答案 你提到的JAXB2Basics不是maven-jaxb2-plu

java - 为非常简单的类实现 `hashCode()`

我有一个非常简单的类,只有一个字段成员(例如字符串)。是否可以实现hashCode()以简单地返回fieldMember.hashCode()?或者我应该以某种方式操纵该字段的哈希码吗?另外,如果我应该操纵它,那是为什么? 最佳答案 如果fieldMember是唯一标识对象的非常好的方法,我会说是。 关于java-为非常简单的类实现`hashCode()`,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

java - 证明 : why does java. lang.String.hashCode() 的实现与其文档相匹配?

java.lang.String.hashCode()的JDK文档famously说:ThehashcodeforaStringobjectiscomputedass[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1]usingintarithmetic,wheres[i]isthe*i*thcharacterofthestring,nisthelengthofthestring,and^indicatesexponentiation.这个表达式的标准实现是:inthash=0;for(inti=0;i看着这个让我觉得我正在通过我的算法类(class)sleep。

java - 计算 java.util.hash 的 hashcode 值时使用的常量说明

谁能解释这些常量的意义以及选择它们的原因?staticinthash(inth){//ThisfunctionensuresthathashCodesthatdifferonlyby//constantmultiplesateachbitpositionhaveabounded//numberofcollisions(approximately8atdefaultloadfactor).h^=(h>>>20)^(h>>>12);returnh^(h>>>7)^(h>>>4);}来源:java-se6库 最佳答案 理解什么是好的哈希函

java - 两个不同的 Class 实例给出相同的 hashCode

我在JBoss服务器上遇到一个奇怪的问题,其中两个类生成相同的hashCode()。Classcl1=Class.forName("fqn.Class1");Classcl2=Class.forName("fqn.Class2");out.println(cl1.getCanonicalName());out.println(cl2.getCanonicalName());out.println(cl1.hashCode());out.println(cl2.hashCode());out.println(System.identityHashCode(cl1));out.printl

java - 为具有浮点成员的类实现 "tolerant" `equals` & `hashCode`

我有一个带有float字段的类。例如:publicclassMultipleFields{finalintcount;finalfloatfloatValue;publicMultipleFields(intcount,floatfloatValue){this.count=count;this.floatValue=floatValue;}}我需要能够按值比较实例。现在我该如何正确实现equals和hashCode?实现equals和hashCode的常用方法是只考虑所有字段。例如。Eclipse将生成以下equals:publicbooleanequals(Objectobj){/

java - 为 hashcode、equals 和 toString 方法生成单元测试

是否有任何工具/库可以自动为我的哈希码和equals方法生成测试,查看这些方法中涉及的实例变量? 最佳答案 Guava使用this用于测试equals和hashCode的测试生成器。 关于java-为hashcode、equals和toString方法生成单元测试,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/10633004/

c++ - 我们可以在 C++ 的类中定义 hashcode 方法吗

我正在尝试用C++实现一个类,我希望每个类都有自己的哈希码实现(基本上将其用作unordered_map和unordered_set)例如:classCustomClass{inta;vectorb;stringc;booloperator==(constCustomClass&o)const{return((a==o.a)&&(b==o.b)&&(c==o.c));}/*Isitpossibletodefinethehashcodefunctionhereinsteadofdefiningitoutsidetheclass.size_toperator()()const{//Some

hadoop - 这对 Text.hashCode() 和 Interger.MAX_VALUE 意味着什么?

最近在看hadoop的权威指南。我有两个问题:1.看到一段自定义Partitioner的代码:publicclassKeyPartitionerextendsPartitioner{@OverridepublicintgetPartition(TextPairkey,Textvalue,intnumPartitions){return(key.getFirst().hashCode()&Interger.MAX_VALUE)%numPartitions;}}这对&Integer.MAX_VALUE意味着什么?为什么要使用&运算符?2.我还想为IntWritable编写一个自定义分区程序