Java & hashcode Role

Source: Internet
Author: User
Tags set set

First, to understand the role of hashcode, you must first know the collection in Java.
In general, there are two types of collections (Collection) in Java, one is list, and the other is set. Do you know the difference between them? The elements within the set are ordered, the elements can be repeated, the latter elements are unordered, but the elements are not repeatable. So here is a more serious problem: to ensure that the elements do not repeat, can two elements should be repeated based on what to judge? This is the Object.Equals method. However, if each additional element is checked once, the number of elements that are added to the collection is much more numerous when the element is many. That is, if there are now 1000 elements in the collection, then the 1001th element joins the collection, it calls the Equals method 1000 times. This obviously will significantly reduce efficiency.
Thus, Java uses the principle of a hash table. The hash (hash) is actually a personal name, and because he proposes a hash algorithm, it is named after his name. A hashing algorithm, also known as a hashing algorithm, is a direct assignment of data to an address based on a particular algorithm. If the hashing algorithm is explained in detail, it will require more articles, which I will not introduce here. Beginners can understand that the Hashcode method actually returns the physical address of the object store (which may not actually be the case).
This way, when the collection is to add a new element, the Hashcode method of the element is called first, and then it can be positioned at the physical location where it should be placed. If there is no element in this position, it can be stored directly in this position without any comparison, and if there is already an element in this position, then the Equals method of calling it is compared with the new element, the same is not saved, and the other address is hashed. So there is a conflict resolution problem here. In this way, the number of actual calls to the Equals method is greatly reduced, almost only one or two times.
So, Java for the Eqauls method and the Hashcode method are defined as:
1, if two objects are the same, then their hashcode value must be the same;
2. If the hashcode of two objects are the same, they are not necessarily the same
The objects mentioned above refer to the comparison with the Eqauls method.
You can of course not do it as required, but you will find that the same object can appear in the Set collection. At the same time, the efficiency of adding new elements will be greatly reduced.

  
The 1.public Boolean equals (Object obj), and Hashcode () methods are methods in the object objects.
The relationship between 2.equals and Hashcode is this:

1, if two objects are the same (that is, with equals to return true), then their hashcode value must be the same;

2. If the hashcode of two objects are the same, they are not necessarily the same (that is, return false with equals)

That
(1) when Obj1.equals (OBJ2) is true, obj1.hashcode () = = Obj2.hashcode () must be true
(2) when obj1.hashcode () = = Obj2.hashcode () is False, Obj1.equals (OBJ2) must be false
3 why rewrite equals?
If you do not override Equals, then the comparison will be whether the object's reference points to the same memory address, which is overridden to compare the value of two objects for equality.
In particular, the use of equals to compare eight wrapper objects (such as int,float, etc.) and the string class (because the class has overridden the Equals and Hashcode methods) object,
The default comparison is the value, which is the comparison reference address when comparing other custom objects
4. What is Hashcode?
Hashcode is a fast access for hashing data, such as when using the Hashset/hashmap/hashtable class to store data,
are judged by the hashcode value of the stored object.
In order to improve the efficiency of the program to achieve the Hashcode method , first hashcode comparison, if different,
That doesn't have to be a comparison of equals, which greatly reduces the number of equals comparisons, which is obvious compared to the large number of efficiency improvements that need to be compared.
A good example is the use of the collection, and we all know that the list collection in Java is ordered, so it can be duplicated, and the set set is unordered,
So it can't be duplicated, so how can we guarantee that it can't be put in duplicate elements, but by the Equals method,
If there are 10,000 elements in the original collection, then put in 10,001 elements, do you want to compare all the previous elements,
See if there is a repetition, European code karma, this efficiency can be imagined, so hashcode should meet and born, Java used a hash table,
Using a hashing algorithm (also known as a hash algorithm), the object data is defined to an address according to the characteristics of the object using a specific algorithm.
Then in the later definition of the incoming data as long as the corresponding hashcode address on whether there is a value , then the equals comparison, if not directly inserted,
As long as the use of equals is greatly reduced, the efficiency of execution is greatly improved. Continue to the above topic, why must rewrite the Hashcode method,
In fact, it is simply to ensure that the same object is guaranteed to have the same hashcode value in the same condition as equals.
If you override equals without overriding the Hashcode method, you might have two objects that do not have a relationship with the same equals
(because equal are rewritten based on the characteristics of the object), but Hashcode is really not the same
5. So how to guarantee this!!
So if we rewrite the euqals for an object, it means that as long as the object's member variable values are equal then Euqals equals True.
But do not rewrite hashcode, then we are new to another object, when the original object. Equals (New Object) equals True, the hashcode of the two are different
, resulting in inconsistent understanding, such as when storing a hash set (such as a set Class), will store two objects of the same value, causing confusion,
Therefore, it is also necessary to rewrite the hashcode ()
Overriding the Equals method when you want to compare the contents of an object without comparing references, but overriding the Equals method must override the Hashcode method, and the Java mechanism calls the Hashcode method automatically when calling the Equals method
hashcode hashcode
In HashMap, if you want to compare keys for equality, use both functions at the same time! Because the Hashcode () method of the custom class inherits from the object class, its Hashcode code is the default memory address , so that even if there are two objects of the same meaning, the comparison is not equal, for example, two "sheep" objects are generated, The normal understanding of these two objects should be equal, but if you do not rewrite the Hashcode () method, the comparison is not equal!
Object Class (JDK API 1.6.0)
Tostring
toString ()
Returns the string representation of the object. Typically, the toString method returns a string that represents this object as text. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method.

ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:

GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

Return:
the string representation of the object.
 

Java & hashcode Role

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.