- If you override the euquals method of a subclass, why should you override the hashCode method
public class HashCodeUse { public static void main(String[] args) { User user1 =new User("A", "female", 10, 100); User user2 =new User("A", "female", 10, 100); System.out.println(user1 == user2); // output:false System.out.println(user1); //output:com.study.day15.part2.User@17b6ded System.out.println(user2); //output:com.study.day15.part2.User@17b6ded System.out.println(user1.equals(user2)); // true HashMap hashMap = new HashMap(); hashMap.put(user1, "user1 of value"); System.out.println(hashMap.get(user2)); // output: value of user1 } } class User{ String name; String gender; int age; int score; public User(String name, String gender, int age, int score) { this.name = name; this.gender = gender; this.age = age; this.score = score; } // Override the equals method of the User class @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return age == user.age && score == user.score && Objects.equals(name, user.name) && Objects.equals(gender, user.gender); } // The hashCode method is also overridden @Override public int hashCode() { return Objects.hash(name, gender, age, score); } } // The euqal method will be used in the use of HashMap. If the hashcode value is not rewritten, hash (user1) will appear // If it is not equal to hash(user2), but equals is equal, a conflict will occur;
-
Gradually understand the relevant concepts of hashMap:
-
Data structure based on hash algorithm: HashMap, HashSet, hashtable
-
First, understand the hash table (logical structure)
The hash table takes the hash function h as the corresponding relationship. The purpose is to directly obtain the desired result at one time without traversal and comparison when querying the target record. Therefore, a hash function relationship h is established between the target record and its keywords, so that each keyword corresponds to a unique storage location in the structure. Thus, given the keyword key and the corresponding relationship h, find the target record value through h(key);
-
Hash table is a logical structure, and hashMap and hashTable are data structures in Java. They realize the corresponding operations by implementing hash table;
-
hashMap
put(key,value) and get(key) methods in HashMap. HashMap is implemented using hash algorithm and based on array + linked list + red black tree. The initial length of the internal array of HashMap is 16 and can be expanded automatically. Explore the implementation process and principle of put(key,value) and get(key)
-
Principle of put(key,value)
When the put method is called, the hash(key) method will be called inside the method to calculate the hash value h of the key, and then the length of the main part (array tab) of the HashMap will be used for bit sum operation (length-1) &h to obtain a value of index = (length-1) & H, and then the Node object can be put into the corresponding tab[index]=Node directly according to the value of index
-
Principle of get(key)
In general, first call the method hash(key) according to the key value, calculate the index value by summing with the array length bits, and then directly return tab [index] Value is enough
However, when the Node object at the calculated index is a linked list structure or a red black tree, Node will be used at this time Next to traverse and compare whether the key value of each Node object Node on the linked list is equal to the passed in key until it matches.
-
-
Concept map of hashMap
- The equals method is selected in the HashMap design. Therefore, for different instantiated objects, if the parent class equals method is overridden instead of the corresponding hashCode () method, the objects with the same attribute may have different hashCode () values. Observe the get(key) operation of HashMap and use Node Next to successively traverse and compare whether the key value of each Node object Node on the linked list is equal to the passed in key, that is, the equals() method of key is used. In this case, if your class overrides the equals() method and does not manage the hashCode () method, in the put(key) method, rely on the hashCode to calculate the index, The original equals() objects will be placed in different indexes, so that the subsequent overwrite operation will not work. As a result, equal objects are placed in different locations rather than overwritten.