草庐IT

equals-operator

全部标签

java - 从抽象类派生时如何遵守equals()的契约

在他的EffectiveJava一书中,JoshuaBloch描述了当派生类向检查中添加额外字段时,equals()的约定会出现的陷阱。通常,这会破坏对称性,但Bloch指出“您可以将值组件添加到抽象类的子类,而不会违反equals契约”。显然这是真的,因为不能有抽象类的实例,所以不存在可违反的对称性。但是其他子类呢?我写了这个例子,故意省略哈希码实现和空检查以保持代码简短:publicabstractclassVehicle{privatefinalStringcolor;publicVehicle(Stringcolor){this.color=color;}publicStrin

java - Java 可以帮助我避免 equals() 中的样板代码吗?

我以Java7的方式实现equals():@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;MyClassother=(MyClass)obj;returnObjects.equal(myFirstField,other.myFirstField)&&Objects.equal(mySecondField,other.mySecondField);}有没有办法减少代码重复?我更

java - java.lang.reflect.Method.equals(Object obj) 中的名称比较

下面是Java7中java.lang.reflect.Method.equals(Objectobj)的实现:/***Comparesthis{@codeMethod}againstthespecifiedobject.Returns*trueiftheobjectsarethesame.Two{@codeMethods}arethesameif*theyweredeclaredbythesameclassandhavethesamename*andformalparametertypesandreturntype.*/publicbooleanequals(Objectobj){if

java - 为什么 .equals() 方法没有被 Java 中的基元数组覆盖?

我目前正在从事一个项目,我想在该项目中使用与数据库进行比较的用户名和密码来实现登录机制。我有这样的想法:publicbooleanverifyUser(Stringusername,char[]password){Listdbpass=getPasswords(username);if(dbpass.contains(password)){overwriteWithNonsense(password);returntrue;}overwriteWithNonsense(password);returnfalse;}当我注意到我的单元测试失败时。所以我更深入地研究了它,注意到Object

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/

java - Jenkins hudson.util.IOException2 : remote file operation failed

我使用的是CentOS5和Jenkins1.430。当我尝试构建时,出现错误:hudson.util.IOException2:remotefileoperationfailed:/home/build/jenkins/workspace/testsathudson.remoting.Channel@6c89db9a:build-testathudson.FilePath.act(FilePath.java:754)athudson.FilePath.act(FilePath.java:740)athudson.scm.SubversionSCM.checkout(Subversion

java - Java 8's HashMap misbehaves if the keys implement Comparable in a way that isn' t 与equals一致是不是bug?

我知道从Java8开始,如果HashMap有足够多的哈希冲突,并且键实现了Comparable,它会useabalancedtreeinsteadofalinkedlistforthebin.但据我所知,Comparable接口(interface)doesnotrequirecompareTo()应“与equals()一致”(尽管强烈建议这样做)。我错过了什么吗?似乎新的实现允许HashMap违反Map接口(interface)的要求,如果键恰好具有兼容但不推荐的Comparable实现。以下JUnit测试在OpenJDK8u72上暴露了此行为:importstaticorg.jun

LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

文章目录一、题目二、题解一、题目Youaregivena0-indexedarrayarrconsistingofnpositiveintegers,andapositiveintegerk.ThearrayarriscalledK-increasingifarr[i-k]Forexample,arr=[4,1,5,2,6,2]isK-increasingfork=2because:arr[0]arr[1]arr[2]arr[3]However,thesamearrisnotK-increasingfork=1(becausearr[0]>arr[1])ork=3(becausearr[0]>

java - operator == 包装类对象的不同行为

任何人都可以向我解释输出中发生了什么。如果==用于比较两个ref。变量它只是检查它的引用是否相同然后它进入ifbody,那到底为什么如果创建静态方法valueOf()和ee==ff不相等(这没问题)如果使用new关键字创建其对象,则aa==bb相等?staticvoidmain(Stringargs[]){Integeraa=Integer.valueOf("12");Integerbb=Integer.valueOf("12");if(aa==bb)System.out.println("aa==bb");if(aa!=bb)System.out.println("aa!=bb");