, whether the values in memory for the two variables are equal (whether they point to the same storage space), you can compare them with the "= =" operator. However, if you want to compare the contents of these two objects to be equal, then the "= =" operator cannot be implemented.
2 equals is one of the methods provided by the object class. Each Java class is integrated from the object class, so each obje
);}}
In hashmap, get () is used to obtain the value, put () is used to insert the value, and containskey () is used to check whether the object already exists. It can be seen that, compared with the arraylist operation, hashmap not only indexes its content through the key, but also has little difference in other aspects.
As mentioned above, hashmap is based on hashcode. There is a hashcode () method
hashcode values must be the same;
2. If the hashcode of the two objects is the same, they are not necessarily the same (that is, false is returned for comparison with equals)
Self-understanding: the hashcode method is implemented to improve program efficiency, and the comparison of
From the beginning of learning Java, from each senior, all kinds of books, the major sites to hear, see, rewrite the Equals method must override the Hashcode method. Overriding the Equals method must override the Hashcode method. Overriding the
First, let's talk about the recommended situation: for example, when your object wants to be put into a set or a map key (non-Hash set and map, such as treeset and treemap ), then you must override the equals () method to ensure uniqueness. Of course, in this case, you do not want to rewrite the hashcode () method, and there is no error. However, for a good programming style, you should rewrite the
Java Collection-hashCode, java-hashcodeRole of hashCode
There are two types in the Java Set: List and Set. The difference between them is that the element division in the List Set is ordered and can be repeated, elements in the Set are unordered and cannot be duplicated. The
We all know that the Java language is fully object-oriented, and in Java, all objects are inherited from the object class.The Equals method compares the addresses of two objects to the address, Hashcode is a local method, and returns the object address value. There are two methods
Default equal implementation:The equal defined in the object class is only a reference judgment. If the two instances are referenced differently, the logic is also different.Equal customization implementation:However, two instances are different, but they are logically the same. For example, the date of some value objects and the user retrieved in oau2. if the UID of these user objects is the same, the logic is considered equal.Keywords: Same logic
Equal uses getclass: If instanceof is not use
classes that do not override the two methods. string equals ()
The same is true if the memory address is the same and the content is the same.The storage address is different to satisfy the length, the hash code, the corresponding characters are the same to return true. hashcode of String ()
@Override public int hashcode () {
int hash =
Hashcode method, the first hashcode comparison, if different, that does not need to be in the comparison of equals, so significantly reduce the equals comparison
The number of times, this contrast needs to compare the number of significant efficiency improvement is very obvious, a good example is the use of the set;
The Equalsbuilder and Hashcodebuilder classes in the Apache Commons Lang Library can simplify the process of rewriting the equals and Hashcode methods in the Java class.
The effective Java book provides an effective way to change the Equals and
1.Object classThe object class is the parent class for all classes in Java, and all classes in Java are directly or indirectlyInherit from Object classThere are not many methods defined in object, because Java has a variety of classes, their totalNot much, object only defines the commonalities that all classes should haveAll classes are subclasses of object, so a
Java object objects have 9 methods, which equals() and hashCode() in the implementation of HASHMAP play a more important role, I studied the source of HashMap, I met them, this blog is mainly to record their love between the killing.In order to illustrate their relationship, we need hashmap background knowledge.How the HashMap is storedHashMap is implemented as a
+ AreaCode; * result + prefix; * result + linenumber; return result;}Now, using the previous test code, I found that I was able to return Jenny.If a class is immutable and the cost of calculating the hash code is large, you should consider caching the hash code inside the object instead of recalculating the hash code every time the request is made, and if most of these objects are used as hash keys, you should calculate the hash code when the instance is created, or you can choose
(INT Arg, int Max, string name ){If (ARG }Public Boolean Equals (Object O ){If (O = This) reutrn true;If (! (O instanceof phonenumber) return false;Phonenumber Pn = (phonenumber) O;Return Pn. Extension = extension Pn. Exchange = exchange Pn. areacode = areacode;}// No Hashcode Method...}There are currently the following lines of programs:Map M = new hashmap ();M. Put (New phonenumber (1, 2, 3), "Jenny ")
Equals (), hashcode (), Cloning learning experienceIn fact, in the development time, rarely to rewrite the Equals (), Hashcode () method, but sometimes the business needs to be rewritten.Attention:Overriding the Equals () method is sure to override the
A rule in Java that determines whether two objects are equal:First, determine whether the hashcode of two objects is equalIf not equal, neither object is considered equalIf equal, determine whether two objects are equal with the equals operationIf not equal, neither object is considered equalIf equal, two objects are considered equalWe need a downward transformat
2015 "Oracle Cup" national Java Program Design Competition-pre-match simulation from the Competition Test Network (http://www.jingkao.net/)Review the overrides of the Equals () and hashcode () classesWhat is rewriting?(1) The polymorphism between the parent class and the child class, redefining the function of the parent class. If you define a method in a subclas
Hashcode and equals are two methods in the object class, where hashcode () is a local method.
(1) hashcode (): returns an integer Hash Value, which is used in hashmap, hashtable, and other classes.
For example, in hashmap, compare keys for consistency.
e.hash == hash ((k = e.key) == key || key.equals(k))
When using o
Equals () is A method provided by the object class, and it is well known that each Java class inherits from Object, So every object has a equals () method, which we generally override when we use this method.The equals () method in the Object class source code Public Boolean e
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.