Java Foundation (1) ---- hashCode

1. The concept of hashCode

(1) The hashCode method is a method of the Object class. In Java, all classes inherit the Object class by default, that is, all classes have the hashCode method.

(2) hashCode is an int number calculated by jdk according to the storage address of the object, that is, the hash code value of the object, which represents the storage address of the object in memory .

2. The role of hashCode

2.1, the quickness of hashCode search

The existence of hashCode is mainly used for the quickness of search, such as HashTable, HashMap, etc. The hashCode is used to determine the storage address of the object in the hash storage structure ( it is the storage address, not the memory address ).

2.2. How HashSet does not allow repetition

For example, we all know that HashSet is not allowed to repeat, HashSet has 100 elements, then add a new element.

(1) If there is no hashCode

It needs to iterate 100 times and call the equals method 100 times to determine whether the newly added element already exists in the HashSet. As you can imagine, the efficiency is very low.

(2) If there is hashCode

When adding a new element to the HashSet, the HashSet will first call the hashCode method, so that it can locate its storage location, and if there is no element there, it will be saved directly. If there is already an element there, call equals to determine whether the contents of the two elements are the same, if they are the same, they do not exist, and if they are different, they will be hashed to other locations. In this way, when we insert a large number of elements into the HashSet, we can greatly reduce the number of calls to the equals method and improve the efficiency.

2.3. Code example of HashSet collection and hashCode method

import java.util.HashSet;

public class Person {

	private int id;
	private String name;
	
	public Person(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		
		//Used to see when the hashCode method was called and how many times it was called
		System.out.println("hashCode is called, hashCode=" + result);
		
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		
		//Used to see when the equals method is called and how many times it is called
		System.out.println( "equals was called" );
		
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	public static void main(String[] args) {
		HashSet<Person> personHashSet = new HashSet<Person>();
		personHashSet.add(new Person(1, "张三"));
		personHashSet.add(new Person(2, "Li Si"));
		personHashSet.add(new Person(1, "张三"));
		System.out.println("The length of personHashSet is " + personHashSet.size());
	}
	
}
For hashCode adjustment, hashCode = 775881
For hashCode adjustment, hashCode = 843084
For hashCode adjustment, hashCode = 775881
equals is called
length of personHashSet is 2

Every time a Person is added to personHashSet, the hashCode method is called, so that the location where the Person is stored can be directly located, and if there is no other Person there, it will be saved directly. If there is already a Person there, the equals method is called to determine whether the two Persons are the same, if they are the same, they do not exist, and if they are different, they are hashed to other locations. In the example code, when adding a new element to personHashSet, the hashCode method is called anyway, so the hashCode method is called three times. The hashCode of the first Person and the third Person are the same. The equals method is called only when the third Person is added, so the equals method is called once. equals is true, the same does not exist, so the length of personHashSet is 2.

(Note: A more detailed interpretation of HashSet will appear in my later blog post, so I won't overwhelm the subject of this article here.)

3. The relationship between hashCode and equals

(1) equals are equal, hashCode must be equal.

(2) If the equals method is overridden, then the hashCode method should also be overridden, and the object (property in the class) used to generate the hashCode must be the same as that used in the equals method.

(3) hashCode is equal, equals is not necessarily equal. It can only be said that these two objects are "stored in the same blue child" in a hash storage structure (such as Hashtable).

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936768&siteId=291194637
Recommended