Differences between =, equals, and hashcode in Java and the example of rewriting the equals and hashcode methods, using shashcode
1. Override the equals Method Instance part of the code reference http://blog.csdn.net/wangloveall/a
Differences between =, equals, and hashcode in Java and the example of rewriting equals and hashcode (conversion) and mongoshashcodeJava =, equals, hashcode difference and override
Learning notes-JAVA-test site 10-Under what conditions do I need to rewrite the equals and hashcode () Methods ?, -Java-hashcode
I. Under what circumstances do I need to rewrite the equals and
In Java:= = is an operator that compares two variables for equality.Equals, is a method of the Objec class that compares two objects for equality, and the Equals method of the Default object class is to compare the addresses of two objects, just as the result of = =. The Equals method of object is as follows: [Java] vi
In Java:= = is an operator that compares two variables for equality.equals, is a method of the Objec class that compares two objects for equality, and the Equals method of the Default object class is to compare the addresses of two objects, just as the result of = =. The Equals method of object is as follows: public boolean
In Java:= = is an operator that compares two variables for equality.Equals, is a method of the Objec class that compares two objects for equality, and the Equals method of the Default object class is to compare the addresses of two objects, just as the result of = =. The Equals method of object is as follows: public boolean
Of course not. The hashcode and equals methods can be rewritten. If one of them is overwritten, but the other is not overwritten, this conclusion is obviously incorrect.
The Code is as follows:
Public class test {
The running result is as follows:
True
Of course, we 'd better rewrite the hashcode method when rewriting the eq
The Equals method and the Hashcode method in Java are in object, so each object has these two methods, sometimes we need to implement the specific requirements, we may have to rewrite the two methods, today we will introduce some of the effects of these two methods.
The Equals () and
hashCode() the implementation of the General method, equal objects, their hash code must be equal.Back to Tophashcode () method detailedhashCode()method returns a hash code value to the object. This method is used for hash tables, such as HashMap.The nature of this is:
During the execution of a Java application, if the information provided by an object to equa
Java-equals () and hashCode () Methods
The java. lang. Object Class has two very important methods:
12
public boolean equals(Object obj)public int hashCode()
ObjectClass is the basis of the class inheritance stru
The equals and hashCode methods in Java are in the Object, so each Object has these two methods. Sometimes we need to implement specific requirements, and we may need to rewrite these two methods, today we will introduce the functions of these two methods.
The equals () and hashCo
Transferred from: http://blog.csdn.net/fenglibing/article/details/8905007 Feng Libin's BlogThe following is an official document definition for hashcode:
The Hashcode method returns the hash code value of the object. This method is supported to provide some advantages to the hash table, for example, the Hashtable provided by Java.util.Hashtable.
The general agreement of
equals () method to compare the content. Of course, the basic type is to compare values. There is nothing to say about it.We should also note that the requirements of the Java language for equals () are as follows, which must be followed:? Symmetry: If X. Equals (y) returns "true", then Y.
a comparison of the content. Of course, the base type is a comparison of the values.It should be noted that the Java language requirements for Equals () are as follows, and these requirements must be followed:1. Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".2. Reflectivity: X.equals (x) must return is "true".3. Analogy: If X.equals (y) returns "true" and Y.equals (z)
Each Java object has a hashcode () and Equals () method. Many classes ignore (Override) default implementations of these methods to provide deeper semantic comparability between object instances. In the Java Philosophy and Practice section, Java Developer Brian Goetz introdu
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
classes are also suitable for this principle. Of course, the class that has not been overwritten will also follow this principle after it inherits the equals () and hashcode () Methods of the object class.
4. When talking about hashcode () and equals (), we can't help but talk about the usage of hashset, hashmap, and
. And so on, you can know that the overridden equals () and Hashcode () methods in a wrapper class such as Integer and double are also appropriate for this principle. Of course, there are no overridden classes that follow this principle after inheriting the Equals () and Hashcode () methods of the object class.4. When
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.