|
If you want to find a collection contains a certain object, you need this object and this set of each object in turn to compare and judge, until you find the object, or until all the objects are relatively far once (if the last an object is an object you are looking for, or the collection does not contain the object you want to find). When the number of objects in the collection are more efficiency is very low. To improve efficiency, Hash algorithm is proposed. Hash algorithm for each object code to calculate a Hash, Hash code assigns an object to a storage area in accordance with, for example, contains a collection of a lot of people, based on nationality, the Chinese people is a storage area, a storage area Americans British is a storage area, ....... So if you want to find out whether the collection has a Chinese people, Chinese people went to the storage area to compare on the line, thereby greatly enhancing efficiency.
Java implemented Hash collection is HashSet. When HashSet find an object, first with hashCode () method to calculate the object's Hash code, then according to Hash code to the appropriate storage area with equals () method to find, thus increasing efficiency. Because it is set, so the same object can have only one.
hashSet example is shown below:
package my;
import java.util.HashSet;
import java.util.Set;
class Person {
// Sex
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);
}
}
The above example first defines the Person class, and then define a HashSet, and added 5 Person to the collection, in which one person was added twice, the results are as follows:
Jack is seen the same person, but it appears twice in the collection, which is what causes it? This is because, Person is a subclass of Object, and Object class equals () method is based on the object's memory address to determine whether two objects are equal, due to the insertion of two Jack memory address is certainly not the same, so the judge the result is not equal, so the two are inserted. So, we need to override the equals () method to determine if two objects are the same object.
// Override equals method
public boolean equals (Object obj) {
// Address equal, certainly the same object
if (this == obj) {
return true;
}
// Types are different, certainly not in the same class object
if (! (obj instanceof Person)) {
return false;
}
// Same type, downcast
Person per = (Person) obj;
// If two objects of the same name and gender, it is the same person
if (this.name.equals (per.name) && this.sex.equals (per.sex))
return true;
return false;
}
Jack still visible was inserted twice, which is what causes it? This is because the Hash Object code is returned Hash object address, and the two objects Hash address is certainly not equal, so the object is inserted 6 times in six storage area, equals () method no storage operation. So, we need to override hashCode () method, according to the name of the object to calculate the Hash code.
// Override hashCode method
public int hashCode () {
return this.name.hashCode ();
}
Visible, Jack inserted only once, and finally correct. If calculated according to sex objects Hash code, the result is correct, Jack will only be inserted once. However, if the two objects of different gender, as follows:
mySet.add (new Person ( "Jack", "Male", 190.0,95.0));
mySet.add (new Person ( "Jack", "Female", 190.0,95.0));
This is because although the two objects of the same Hash code (either by name or by gender to calculate, Hash codes are the same), but the equals () method to determine which two objects are not equal, so are inserted. |
|
|
|