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
returns the computed Hashcode value:1 public class comhashcode{2 public static void Main (string[] args) throws Exception {3 Comhashcode a = new Comh Ashcode (); 4 Comhashcode B = new Comhashcode (); 5 System.out.println (A.hashcode ()); 870919696 6 System.out.println (B.hashcode ()); 298792720 7 8 Long num1 = new Long (8), 9 long num2 = new Long (8), System.out.println (Num1.
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
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
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 ...
); 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
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
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
The use of map is usually a Java native type, so there is little focus on the internal implementation of the methods of Hashcode () and Equals (). The recent realization of a small tool, involving their own writing of the class lookup, and again revisit the relevant knowledge.On the simple example code, the difference between the overwrite and the non-overwriteDo
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
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
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
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
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
equals ().The Hashcode () method in Objecct,The Hashcode () method acts as a hash of the object, and in the Hashmap,hashset,hashset collection, the effect of the random access element can be achieved. The use of hash values (Hashcode ()) reduces the number of comparisons between elements as much as possible.1 Public
One, the same pointare used to compare values or objects.Two, different pointsfor "= =", for the base type (Char,byte,short,int,long,float,double,boolean), the comparison is the value, so is equal, for the reference object, compared to the referenced object's heap address, for example: Public class Main { publicstaticvoidthrows interruptedexception { New Object (); New Object (); = = obj2);} }Note For wrapper classes such as Integer, there are some special.For 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
The content of this chapter mainly addresses the following issues:
What is the role of 1 equals ()?
What is the difference between 2 equals () and = =?
What is the role of 3 hashcode ()?
4 What is the connection between hashcode () and Equals ()?
Role of the 1th part
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.