Java HashSet class and JavaHashSet class
If you want to check whether an object is included in a set, you need to compare and judge the object and each object in the set until the object is found, or compare all objects once (if the last object is the object to be searched, or the set does not contain the object to be searched ). When the number of objects in the set is large, the efficiency is very low. To improve efficiency, a Hash algorithm is proposed. The Hash algorithm calculates a Hash code for each object and assigns the object to a storage area based on the Hash code. For example, a set contains many people, depending on the nationality, the Chinese are a storage area, the Americans are a storage area, and the British are a storage area ,....... In this way, if you want to check whether the set contains a certain Chinese, you can compare it with the Chinese storage area, which greatly improves the efficiency.
The Hash set implemented in Java is HashSet. When a HashSet looks for an object, it first uses the hashCode () method to calculate the Hash code of the object, and then uses the equals () method to search for the corresponding storage area based on the Hash code, this improves the efficiency. Because it is a set, only one of the same objects can be created.
The example of hashSet is as follows:
Package my; import java. util. hashSet; import java. util. set; class Person {// gender String sex; // name String name; // height Double hei; // weight Double wei; public Person (String n, String s, double h, Double w) {this. name = n; this. sex = s; this. hei = h; this. wei = w;} public String toString () {return "\ n name:" + this. name + "Gender:" + this. sex + "height:" + this. hei + "Weight:" + this. wei ;}} public class myHS {private static Set <Person> mySet = new HashSet <Person> (); public static void main (String [] args) {mySet. add (new Person ("Tom", "Male", 170.0, 70.0); mySet. add (new Person ("Peter", "Male", 175.0, 70.0); mySet. add (new Person ("Kate", "Female", 168.0, 60.0); mySet. add (new Person ("Alice", "Female", 161.0, 55.0); mySet. add (new Person ("Jack", "Male", 190.0, 95.0); mySet. add (new Person ("Jack", "Male", 190.0, 95.0); System. out. println (mySet );}}
In the preceding example, the Person class is defined, a HashSet is defined, and five persons are added to the set. One Person is added twice. The running result is as follows:
It can be seen that Jack is the same person but appears twice in the collection. Why? This is because Person is a subclass of the Object, and the equals () method of the Object Class determines whether two objects are equal based on the Object's memory address, because the memory address of the two inserted Jack is definitely not the same, the result is not equal, so the two inserted. Therefore, we need to override the equals () method to determine whether two objects are the same object.
// Override equals method public boolean equals (Object obj) {// The address is equal, it must be the same Object if (this = obj) {return true ;} // if the type is different, it is certainly not the same class object if (! (Obj instanceof Person) {return false;} // The type is the same, and the downward transformation Person per = (Person) obj; // if the names and gender of the two objects are the same, if (this. name. equals (per. name) & this. sex. equals (per. sex) return true; return false ;}
After overwriting the equals () method, the running result is as follows:
It can be seen that Jack is still inserted twice. Why? This is because the Object's Hash code returns the Object's Hash address, and the Hash addresses of the two objects are definitely not equal, so the objects inserted six times are stored in six storage regions, the equals () method does not run at all. Therefore, the hashCode () method needs to be overwritten to calculate the object's Hash code based on the name.
// Override the hashCode method public int hashCode () {return this. name. hashCode ();}
The running result is as follows:
It can be seen that Jack is inserted only once and is correct. If the Hash code of an object is calculated based on gender, the result is correct. Jack is inserted only once. However, if the two objects have different genders, they are as follows:
mySet.add(new Person("Jack","Male",190.0,95.0)); mySet.add(new Person("Jack","Female",190.0,95.0));
Both objects will be inserted:
This is because although the two objects have the same Hash code (both by name and by gender), The equals () method determines that the two objects are not equal, so they are all inserted.