Many people have studied Java for a long time, but have not understood the role of the Hashcode method,Let me explain. First of all, to understand the role of hashcode, you have to know the collection in Java first.In general, the set in Java (Collection) has two classes, on
and equals is the difference is that he returned to the int type, compared to not intuitive. We generally overwrite equals while covering the hashcode to make their logic consistent. For example, if the name and gender are equal even if 2 objects are equal, then the Hashcode method also returns the
There are many people who have studied Java for a long time, but have not understood the role of the Hashcode method,Let me explain it to you. 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 o
":
The General agreement of Hashcode is:
1. During Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the object to equals
must create a matching hashcode implementation! In addition, implementation of equal should be implemented according to our implementation, which may result in the absence of the same hash code, because they are using an Object implementation.Hashcode conventionsReferences from the original document:General conventions for Hashcode:
In a Java applicatio
The Hashcode method returns the hash code value of the object.The Hashcode () method can be used to improve the efficiency of the search in the map, map will be based on different hashcode () in different locations, map in the search for an object by Hashcode () to find the corresponding position, and then according to
from the beginning of learning Java, Hashcode () method is the object class itself has the method, all classes inherit the object, also Hashcode () this method. When you are learning Java, you are told to override the Hashcode method when overriding the
In Java, there is a rule that two identical objects (that is, the equals operation is true) and their hash code must be the same. There is a Hashcode method in the object class that you can call to view the object's hash code. The following examples illustrate.
Copy Code code as follows:
Package test;
public class Test {
public static void
++];
}
hash = h;
}
return h;
}
hash function
/**
* Applies a supplemental hash function to a given hashcode, which
* Defends against poor quality hash functions. This is critical
* Because HashMap uses power-of-two length hash tables, that
* Otherwise encounter collisions for hashcodes this do not differ
* in lower bits. Note:null keys always maps to hash 0, thus index 0.
*/
tatic int hash (int h) {
//This function en
discards the element without saving, and hashes to other addresses.
2.HOW use Hashcode ()?
The Java language equal () has five requirements that must be followed for ape design.
of symmetry. If a.equal (b) returns "true", b.equal (a) must also return "true".of the reflex. A.equal (a) must return "true".Transitivity. If a.equal (b) returns "true" and B.equal (c) returns "true", C.equal (a) will return "t
.equals (I4) = true
I2 = I4 = falseI2.equals (I4) = true
I = I2 = trueI1.equals (I) = true
I = I4 = trueI4.equals (I) = true
Iii. How to Use equals and = for other classesMost of the classes in the API are overwritten by the equa
hashcode.You want to get HashMap's Value,hashmap by using an object key.Find the address in memory through the hashcode of your incoming object,When this address is found, then the Equals method is used to compare whether the contents of this address are the same as the one you put in, and the value is taken out.So here's to match 2 parts, hashcode and equalsBut
an object key.Find the address in memory through the hashcode of your incoming object,When this address is found, then the Equals method is used to compare whether the contents of this address are the same as the one you put in, and the value is taken out.So here's to match 2 parts, hashcode and equalsBut if you say you new object as key to get value is never th
contents of this address are the same as the one you put in, and the value is taken out.So here's to match 2 parts, hashcode and equalsBut if you say you new object as key to get value is never the result,Because every time new an object, the hashcode of this object is always different, so we're going to rewrite hashcode,You can make your
The following is a java4android video tutorial from marschen
main content of this set. 1. role of hashcode 2. implementation of hashcode. 3. function of tostring 4. tostring implementation method. what is hash algorithm . data of any length ====> (hash algorithm) ==> hash value (fixed length) input 1 ====================>> hash value 1 input 2 ======== ========>> hash value 2 input 3 =========
= count; for (int i = 0; i H = 31*h + val[off++];}hash = h;}return h;}It is not related to the memory address. This is done to ensure that two objects that are returned as true are compared with equals, and that their hashcode are the same.So when you rewrite equals, you rewrite Hashcode (). Of course, this is equival
First look at the implementation of the Hashcode method in string1 Public inthashcode () {2 inth =Hash;3 if(h = = 0 value.length > 0) {4 CharVal[] =value;5 6 for(inti = 0; i ) {7H = * H +Val[i];8 }9hash =h;Ten } One returnh; A}In the string class, there is a private instance field hash representing the hash value of the string, the hash value of the strings is comp
1. The Hashcode () method of the object class is as follows:public int hashcode (): Returns the hash code value of the object, which is related to the address value, but not the actual address value (the hash value is an integer value converted from the actual address value), which you can interpret as the address value.2. The GetClass () method of the object class is as follows:Public final Class getclass
;for (int i = 0; i H = 31*h + val[off++];}hash = h;}return h;}It is not related to the memory address. This is done to ensure that two objects that are returned as true are compared with equals, and that their hashcode are the same.So when you rewrite equals, you rewrite Hashcode (). Of course, this is equivalent to a
Overview:
A.= Can be used for basic types and reference types: when used for basic types, compare whether the values are the same; when used for reference types, compare whether the objects are the same.
B.For string a = "A"; integer B = 1; this type of special object creation method, the value is the same when it is =.
C.The basic type does not have the equals method. Equals only compares whether the
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.