Problem caused by hashCode (). Why hashcode will be cached ? What is the use of hashcode in Java? 该机制并不基于实体类中的equals和hashcode 方法。 This class does not implement own equals and hashCode methods.
Practice with equals and hashCode . Question 24: What are the functions of hashCode () method? Significance of equals and hashcode . 在Java中重写equals和hashCode 时应考虑哪些问题?? What issues should be considered when overriding equals and hashCode in Java? Equals方法、hashCode 方法被定义在Object类中。 The equals and hashcode are methods defined in the Object class. 而且name是个字符串,所以它已经在内部缓存了hashCode ()的值。 Also, the name is a string, so it has already a cached hashCode () value inside. 解决方法很简单,只需提供hashCode ()和equals()实现即可。 The solution is simple- it's crucial to provide the hashCode () and equals() implementations.和setter/getter、hashcode /equals以及constructors/toString这样的模板代码说再见。 Say goodbye to the boilerplate code of setter/getter, hashcode /equals, constructors/toString. 因为字符串是不可变的,所以在它创建的时候hashCode 就被缓存了,不需要重新计算。 Since String is immutable, its hashcode is cached at the time of creation and it doesn't need to be calculated again. 由于这些类都是不可修改的并且可以实施hashCode ()和equals(),它们都可以做为很好的散列关键字。 Because these classes are immutable and implement hashCode () and equals() sensibly, they all make good hash keys. 注意,如果你决定定义一个元素是联合元素的Set,正确地实现equals()和hashCode ()是非常重要的。 Note: if you define a Set of composite elements, it is very important to implement equals() and hashCode (). 字符串的不可变性保证hashcode 始终相同,所以hashcode可以被缓存而不用担心字符串被修改。 Being immutable guarantees that hashcode will always be the same so that it can be cashed without worrying about the changes. 注意,如果你定义的Set包含组合元素(composite-element),正确地实现equals()和hashCode ()是非常重要的。 Note: if you define a Set of composite elements, it is very important to implement equals() and hashCode (). 实施equals()和hashCode ()有一些限制,Object文件中列举出了这些限制。 There are some restrictions placed on the behavior of equals() and hashCode (), which are enumerated in the documentation for Object. String、enum、class或annotation的成员值I的哈希码v是通过调用v.hashCode ()来计算的。 The hash code of a string, enum, class, or annotation member-value I v is computed as by calling v. hashCode (). 在Kotlin中,你可以简单地使用数据类,编译器会自动派生equals()和hashCode ()等方法。 In Kotlin, you can simply use data classes instead, where the compiler automatically derives methods like equals(), hashCode () and many more. 根据您使用的类,您可能希望降低superclass的equals()或hashCode ()功能一部分计算能力。 Depending on your class, you may also want to delegate part of the computation to the equals() or hashCode () function of the superclass. 如果重载equals()方法,则可以改变两个对象的等同方式,并且Object的hashCode ()实现不再有效。 If you override the equals()method, you change the way two objects are equated and Object's implementation of hashCode ()is no longer valid. 对于比较复杂的类来说,equals()和hashCode ()的行为可能甚至受到superclass或interface的影响。 For more complex classes, the behavior of equals() and hashCode () may even be imposed by the specification of a superclass or interface. 我从定义一个简单的Java类开始,这个类包括一些字段、toString、equals、hashCode 等,大约写了50行代码。 I started by defining a simple Java class with a few fields, toString, equals, hashCode etc. It was about 50 lines of code. HashCode ()的需求更特殊--list的hashCode ()值必须符合以下计算:.The requirements for hashCode () are defined with even more specificity-- the hashCode () value of a list must conform to the following calculation:. Case类还消除了值对象的所有干扰(像equals和hashCode 实现),并同时减少了常见错误来源。 Case classes eliminate all noise from value objects as well(like equals and hashCode implementations) and simultaneously cut down a common source of errors. 简而言之,因为String是不可变的,所以没有人可以在创建后更改其内容,这保证了String的hashCode 在多次调用时是相同的。 String in immutable means, no one can change its contents once created and which guarantee the hashcode of string to be same in multiple invocation. 缺省情况下,equals()和hashCode ()基于标识的实施是合理的,但对于某些类来说,它们希望放宽等式的定义。 An identity-based implementation for equals() and hashCode () is a sensible default, but for some classes, it is desirable to relax the definition of equality somewhat. 出于安全起见,EclipseIDE将equals和hashCode 作为成对的选项出现:Sourcegt;GeneratehashCode ()andequals()……. To be on the safe side, let the Eclipse IDE generate the equals and hashCode functions as a pair: Sourcegt; Generate hashCode () and equals()….".
Display more examples
Results: 80 ,
Time: 0.0197