Summary of usage of Hashcode method and Equals method in Java _java

Source: Internet
Author: User
Tags serialization set set

First of all, to understand the role of hashcode, you must first know the collection in Java.

In general, the set in Java (Collection) has two classes, one is list, and the other is set. The elements in the former set are ordered, the elements can be duplicated, the latter elements are unordered, but the elements cannot be duplicated.

So here is a more serious problem: to ensure that the elements do not repeat, can two elements of repetition should be 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 frequent when the elements are many. That is, if there are now 1000 elements in the collection, then the 1001th element will call the Equals method 1000 times when it joins the collection. This will obviously greatly reduce efficiency.

As a result, Java uses the principle of a hash table. Hash is actually a personal name, and since he proposes the concept of a hashing algorithm, it is named after him. Hash algorithms, also known as hashing algorithms, are used to assign data directly to an address according to a specific algorithm. Beginners can understand that the Hashcode method actually returns the physical address of the object store (which may not actually be).

This way, when the collection wants to add a new element, it first calls the Hashcode method of the element, and then it can navigate to the physical location where it should be placed. If there are no elements in this position, it can be stored directly in this position, no more comparisons are made, and if there is already an element in the position, the Equals method of calling it is compared to the new element, and the same is not saved, and the other addresses are hashed out. So there is a problem of conflict resolution here. The number of actual calls to the Equals method is greatly reduced, almost one or two times.

So, Java for the Eqauls method and the Hashcode method is this stipulation:

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 object mentioned above refers to the comparison with the Eqauls method.) )

You can of course not do it as requested, but you will find that the same object can appear in the set set. At the same time, the efficiency of adding new elements will be greatly reduced.

Hashcode This method is used to identify whether 2 objects are equal. So you would say, isn't there a way to be equals? Yes, these 2 methods are used to determine whether 2 objects are equal. But they are different. In general, this method of equals is called to the user, and if you want to determine whether 2 objects are equal, you can rewrite the Equals method and then call in the code to determine if they are equal. In simple terms, the Equals method is primarily used to determine whether 2 objects are equal or not on the surface or on the content.

For example, there is a student class, the attribute is only the name and sex, then we can think as long as the name and gender equality, then say that these 2 objects are equal. Hashcode method The general user does not go to call, for example in HashMap, because key is not repeatable, he judged the key is not repeated time to judge the hashcode this method, but also used the Equals method. Here can not be repeated is that equals and hashcode as long as there is a range can be! So in simple terms, hashcode is equivalent to an object encoding, as if the file in the MD5, he and equals are different is that he returned to the int type, compared to not intuitive. We generally cover equals while also covering hashcode, so that their logic is consistent. For example, if the name and gender equality even if 2 objects are equal, then the Hashcode method also returns the Hashcode value of the name plus the hashcode value of the gender, so logically they agree. To physically determine whether 2 objects are equal, use = = on it.

In the Java language, the use of equals () and hashcode () two functions is tightly coupled, and if you design one of them yourself, you need to design another one. In most cases, these two functions are not considered, and they can be used directly by their default design. But in some cases, the two functions are best designed to ensure that the entire program works. The most common is when an object is added to the collection object (Collection Object), the two functions must be designed by themselves. A more granular definition is: If you want to put an object A into a second collection object B, or use this object A to find the key to the location of a meta object in the collection of object B, and to support whether or not to hold, to delete objects such as Object B in the collection of such operations, then, Equals () and Hashcode () Functions must be defined by the developer themselves. In other cases, these two functions do not need to be defined.

Equals ():

It is used for comparisons of two objects, a comparison of object content, and, of course, for comparisons of object reference values. What is a comparison of objects see values? Is the two reference variables worth comparing, we all know that the value of the variable is actually a number, which can be considered as a code to identify different objects. Two objects refer to the comparison of the values, that is, two numbers comparison, two code comparison. This comparison is the default object comparison method, which is already in the design of object. So you don't have to rewrite it yourself, wasting unnecessary time.

The comparison of object content is the real purpose of the design equals (), and the Java language requires the Equals () as follows, and these requirements must be followed. Otherwise, you shouldn't be wasting time:

• Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".

• reflectivity:x.equals (x) must return is "true".

• Analogy: if X.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return "true".

• Consistency: if X.equals (y) returns "true", as long as the X and Y contents remain unchanged, no matter how many times you repeat X.equals (y), the return is "true".

• In any case , x.equals (NULL) returns "false" forever, and X.equals (and X objects of different types) always returns "false".

Hashcode ():
This function returns an integer code that is used for the operation of the hash, please do not confuse the code with the code symbol represented by the reference variable mentioned above. The latter is not just a code name but also has the function of locating objects in memory. The value returned by Hashcode () is used to classify the position of an object in some particular collection object. These objects are HashMap, Hashtable, HashSet, and so on. This function and the Equals () function above must be designed to assist HashMap, Hashtable, hashset, etc. to search and locate a large number of objects that they collect.

How do these collection objects work, imagine each Meta object hashcode is a box of code, according to the code, each meta object is according to Hashcode () provided by the Code into the corresponding box. All the boxes add up to be a hashset,hashmap, or Hashtable object, we need to look for a meta object, the code first, is the hashcode () returned the integer value, so we find it in the box, and then in the box, each The meta objects are all taken out and compared to the object we are looking for, if the contents of two objects are equal, our search will end. This operation requires two important information, one is the object's Hashcode (), and one is the result of object content contrast.

The return value of the hashcode () and Equals () are related as follows:

• If X.equals (y) returns "true", then the Hashcode () of x and Y must be equal.

• If X.equals (y) returns "false", then the Hashcode () of x and Y may be equal or unequal.

Why the two rules are such, the reason is very simple, take hashset, HashSet can have one or more boxes, in the same box can have one or more unique meta objects (HashSet must be accommodated in unique meta objects). This example shows that a meta object can have the same hashcode as other different meta objects. However, a meta object can only be equal to a tuple that has the same content. So the two rules must be set up.

The two functions are designed to notice:
If you are designing an object type that is not used in a collection object, then there is no need to redesign the two functions themselves. This is the correct object-oriented design method, any user can not use the function, the first do not design, so as to avoid the future expansion of the function to bring trouble.

If you are in the design of ingenuity, do not adhere to the above two sets of rules, then advised you to do not do such a fantasy thing. I have not encountered any developers and I said that the design of these two functions to violate the previous two rules, I encountered these violations of the rules are treated as a design error.

When an object type is a meta object of a collection object, the object should have its own design to handle equals (), and/or process hashcode (), and adhere to the two principles mentioned above. Equals () first check for null and is the same type. Checking the same type is to avoid the occurrence of classcastexception such as the exception to throw out. Check null is to avoid the occurrence of nullpointerexception such exceptions to throw out.

If your object contains too much data, the two functions equals () and hashcode () will become inefficient. If an object has data that cannot be serialized, Equals () may have an error in the operation. Imagine an object x whose integer data is transient (cannot be serialize into binary data streams). But Equals () and hashcode () all depend on this integer data, so is this object the same before and after serialization? The answer is not the same. Since the integer data before serialization is a valid data, after serialization, the value of the integer data is not stored, and then replaced by the binary data flow after the object, both (object in serialization Before and after the state has been different. This is also to be noted.

Knowing the above can help you:

1. For better design and development.

2. Conduct better test case development.

3. Let the interviewer be satisfied with your knowledgeable during the interview process.

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.