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
directly on the source!public native int hashcode ();/*** Returns a hash code value for the object. This method is* Supported for the benefit of hashes tables such as those provided by* {@link Java.util.HashMap}.* * The general contract of {@code hashcode} are:* * * An execution of a Java application, the {@code Hashcode
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)
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.
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
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
of the returned object (which can be viewed as an address ). Therefore, if the hashcode method is not rewritten, The hashcode of any object is not equal. Generally, the hashcode and equals methods need to be rewritten in the Collection class, because if you need to add an object to the Collection class (such as hashse
. 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
and double encapsulation classes are also suitable for this principle. Of course, it has not been rewritten.Class. This principle is also observed after the equals () and hashcode () Methods of the object class are inherited.
4. When talking about hashcode () and equals (), we can't help but talk about the usage of ha
java = =, Equals (), hashcode () Source Code AnalysisIn Java programming or interview often encounter = =, equals () comparison. I looked at the source code, the actual programming summary.1. = == = In Java is the address that com
. Wait, these classes are all overriding the Equals () method, thus making a comparison of the content. Of course, the basic type is the comparison of values, this is nothing to say.We should also note that the Java language requirements for Equals () are as follows, and these requirements must be followed:• Symmetry: If X.equals (y) returns "true", then Y.equals
Java --- hashCode () and equals (),1. hashCode () and equals () APIs
HashCode() AndEquals() All come from the god classObject,All classes have these two methods: special timing and re-writing.
They are used for comparison in the s
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 the object class 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 refer to the same object (x = = Y has a value of tr
The use of Object objects in Java Learning (Small records in Java learning) Wang Coli (Star stars)Object Objects ( Reference API learning )Focus on three methods:1.toString returns the string representation of the object.2.equals (Object obj) indicates whether another object is equal to this object.3.hashCode () return
Previously published an article about the Equals method rewrite http://www.cnblogs.com/aL0n4k/p/4777333.htmlBelow on the Hashcode method to publish my understanding, for reference only, Exchange.In relation to the Java rewrite Equals method, it has been mentioned that when comparing 2 objects, compare their respective
Note hashcode only when hashtable, hashmap, hashset, and linkedhashmap are used. hashcode is useless in other places. (This is not necessarily true)
Whether two objects are equal requires that hashcode () be equal. Is the following statement true?
In a Java set, the rule for determining whether two objects are equal is
field in the object. Returns true if all of these tests are successful, otherwise false.5. After writing completes the Equals method, check for symmetry, transitivity, and consistency.3, HashcodeThe Hashcode () method returns a numeric value, as can be seen from the name of the method, and its purpose is to generate a hash code. The main purpose of hash code is to hash the object as a key input, it is easy
Java Basic Parsing series (11)---equals, = =, and Hashcode methods directory
Java Basic Parsing series (i)---String, stringbuffer, StringBuilder
Java Basic Parsing series (ii)---integer cache and bin unpacking
Java
1. Background knowledgeThis code is based on jdk1.8 analysis, the Java programming idea has the following description:Another look at Object.java's description of the Hashcode () method:/** * Returns A hash code value for the object. This method was * supported for the benefit of hash tables such as those provided by * {@link java.util.HashMap}. * For the 3-point Convention, translate as follows:1) Dur
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.