Hashcode, the hashcode must be equal.2. HashMap gets an object that compares key hashcode equals and equals to true.The reason why hashcode equal, but can be unequal, such as objecta and OBJECTB they all have attribute name, then
Java-57-Object equals, hashcode, and toString MethodsThe hashCode method of an Object is the most basic. Both the equals and toString Methods indirectly use the hashCode method. Generally, when we override the
TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified.
X,x.equals (NULL) should return FALSE for any non-null reference value.
The Equals method of object implements the most differentiated equality relationship on the object, that is, for any non-null reference value x and Y, this method returns True if and only if X and Y ref
Http://tieba.baidu.com/F? Kz= 879340761
In some cases, we need to determine whether two objects are equal. Every class in Java inherits from the object class. It uses the equals () and hashcode () methods to determine whether two objects are equal.
1. Equals ()Requirements:1 Introspection: returns true for any non-nul
,b.equals (c) True, then A.equals (c) is also true, equivalent to a = B, b = c, then A = C
Consistency: A.equals (b) must always return true whenever the state of A and B objects does not change
A.equals (NULL) to return FALSE
3. PracticeDesigning a class person containing height, weight, age, and Blood is an integer attribute, implements the Hashcode method, orchestrates four attributes into an integer as
I need to record some of the things I saw in Sunday, or I will forget.
Hashcode, equals:
1 Each Java object has a hashcode and equals method.
The ultimate Java class is the object class, so how does the object class label itsel
comparison of the values in the class, do not directly compare two values with equals, for example:Double A = 1;Double b = 1;System.out.prinln (A.equals (b)); It is clear that this statement is wrong.The Java language has the following requirements for Equals ():A: Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".B: reflectiv
non-0 constant value, such as 17, is stored in the int variable result; [2] For each key field F in the object (referring to each domain considered in the Equals method): [2.1]boolean type, calculation (f. 0:1); [2.2]byte,char,short type, calculation (int); [2.3]long type, calculation (int) (f ^ (f>>>32)); [2.4]float type, calculate float.floattointbits(afloat); [2.5]double type, computes double.doubletolongbits(adouble) Get a long, then execute [2
Today to interview, I always feel that I understand everything, should not be a problem, when asked "not overridden by the Equals () method and the Hashcode () method inside the content is. Why do I need to override the Hashcode () method while rewriting the Equals () method? When, to tell the truth, a face Meng Ah ...
in Java, = =, Equals (), and hashcode () are all related to the comparison of objects.
about = == = is easy to understand. Java design Java is to compare two objects is not the same object.For reference variables, the two reference variables refer to the same object whe
); Sbset.add (HELLOSB); Sbset.add (HELLO2SB); Person Person1 = new person (1, "eke"); Person Person2 = new person (1, "eke"); Set personset = new HashSet (); Personset.add (Person1); Personset.add (Person2); Personwithhashcode code1 = new PersonwithhashCode (1, "eke"); Personwithhashcode Code2 = new Personwithhashcode (1, "eke"); Set codeset = new HashSet (); Codeset.add (CODE1); Codeset.add (CODE2); System.out.p
The following content is summarized in objective Java.1. When to rewrite equals ()When a class has its own unique concept of "logical equality" (different from the concept of object identity ).2. design equals ()[1] use the instanceof operator to check whether the real parameter is of the correct type ".[2] for each "key field" in the class, check the field value
? objects.equals (hireday,other.hireday); } public int Hashcode () {the//objects.hash () method provides multiple parameters,//Call the Objects.hashcode () method on each parameter to get the respective hash value and combine the hash values. return Objects.hash (name,salary,hireday); } Public String toString () {return GetClass (). GetName () + "[name" +name+ "salary=" +salary+ "hireday=" +hireday+ "]"; } }Create employee sub-class managerP
method public int hashcode () {///return 1; This method can also be effective but calls the Equals method, and the comparison is much more//the method calls the Hashcode method of String, it does not call Equals method, less number of comparisons return Name.hashcode () +age*39; The uniqueness of the
equal to =, so in most cases it will still go to equals, so equals is equally important. To rewrite it, both of them will be overwritten ~ In fact, java has a convention on hashCode:1. during the execution of an application, if the information used for the equals Method Com
In project development, we all have this experience, that is, when adding a table, the corresponding increase in Java classes, in the Java class, there are a number of common methods, including: Equals (), Hashcode (), toString (), CompareTo () These four methods, For beginners who just touch
these person objects is the same, Object 1.equals (object 2) should return true, regardless of the position of object 1, Object 2 in memory!2, overriding equals Why rewrite hashcode?The reference blog post at the beginning of this article has already mentioned a reason. That is, since object 1.equals (object 2) return
object being stored, and O2 should be replaced with O1, but because O1.hashcode ()!=o2.hashcode (), ( The Hahscode method is called, and the hash list does not have an element at the address of the hash code, then the O1 is placed in the address. This resulted in the existence of two identical objects in the HashMap, violating the design principles of HashMap.2. In a hash list such as Hashmap,hashtable,has
objectsHashcode is a fast access for hashing data, such as using the Hashset/hashmap/hashtable class to store data, based on the hashcode value of the stored object to determine whether the same.So if we rewrite the euqals for an object, it means that as long as the values of the member variables of the object are equal then Euqals equals true, but does not rewrite has
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.