TreeMap, Comparable, and Comparator in Java
We know that the storage location of HashMap is stored according to the hashCode of the key object, while that of TreeMap is stored according to the hashCode, it is stored according to the compareTo method of the implemented Comparable interface. As long as the returned result of compareTo is 0, the two objects are equal, so there will be no two objects, the put method overwrites the previous one. We don't even need to rewrite the equasls and hashCode methods, but just need to implement the Comparable interface to override the comparareTo method, however, we cannot guarantee that HashMap will not be used in the application, so we maintain good habits. When we define an object, we will habitually rewrite the equals and hashCode methods. Test the Comparable interface: The first comparison: defines a User class to implement the Comparable interface. By age, equals is set to true, and hashCode is always equal. Copy the public class User implements Comparable <User> {private String id; private String name; private Integer age; public User () {} public User (String id, String name, integer age) {super (); this. id = id; this. name = name; this. age = age;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name ){ This. name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this. age = age ;}@ Override public String toString () {return "User [name =" + name + ", age =" + age + "]";} @ Override public boolean equals (Object obj) {return true ;}@ Override public int hashCode () {return 0 ;}public int compareTo (User o) {return this. age> o. getAge ()? 1: this. age = o. getAge ()? 0:-1 ;}} copy the code test code: copy the code public class TestUser {public static void main (String [] args) {Map <User, integer> userHashMap = new HashMap <User, Integer> (); User user1 = new User ("1", "Jay", 30 ); user user2 = new User ("2", "Jolin", 21); User user3 = new User ("3", "Jack Cheng", 22 ); user user4 = new User ("4", "Bruce Lee", 22); userHashMap. put (user1, 100); userHashMap. put (user2, 200); userHashMap. put (us Er3, 300); userHashMap. put (user4, 400); System. out. println (userHashMap); Map <User, Integer> userTreeMap = new TreeMap <User, Integer> (); userTreeMap. put (user1, 100); userTreeMap. put (user2, 200); userTreeMap. put (user3, 300); userTreeMap. put (user4, 400); System. out. println (userTreeMap) ;}} copy the code result: {User [name = Jay, age = 30] = 400} {User [name = Jolin, age = 21] = 200, user [name = Jack Cheng, age = 22] = 400, User [n Ame = Jay, age = 30] = 100} conclusion: For HashMap, as long as the equals of keys are equal, the two elements are equal, and HashMap cannot be stored; treeMap does not support equals and hashCode. If compareTo is equal, the two elements are the same and cannot be stored. 2. second comparison: Now we rewrite the hashCode and equals methods by id, as follows: copy the code @ Override public int hashCode () {final int prime = 31; int result = 1; result = prime * result + (id = null )? 0: id. hashCode (); return result ;}@ Override public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; User other = (User) obj; if (id = null) {if (other. id! = Null) return false;} else if (! Id. equals (other. id) return false; return true;} copy the code to test the code. The test code remains unchanged. The result is {User [name = Jolin, age = 21] = 200, user [name = Jay, age = 30] = 100, User [name = Bruce Lee, age = 22] = 400, User [name = Jack Cheng, age = 22] = 300} {User [name = Jolin, age = 21] = 200, User [name = Jack Cheng, age = 22] = 400, user [name = Jay, age = 30] = 100} Note: As long as HashMap equals does not exist, it indicates inequality. For TreeMap, if compareTo is equal, then the two elements are equal, the sorting rules are defined by the compareTo method. Next, add List in the test code to test: copy the code List <User> userList = new ArrayList <User> (); userList. add (user1); userList. add (user2); userList. add (user3); userList. add (user4); System. out. println (userList); Collections. sort (userList); System. out. println (userList); copy the code result: {User [name = Jolin, age = 21] = 200, User [name = Jay, age = 30] = 100, user [name = Bruce Lee, age = 22] = 400, User [name = Jack Cheng, age = 22] = 300} {User [name = Jolin, Age = 21] = 200, User [name = Jack Cheng, age = 22] = 400, User [name = Jay, age = 30] = 100} [User [name = Jay, age = 30], User [name = Jolin, age = 21], User [name = Jack Cheng, age = 22], User [name = Bruce Lee, age = 22] [User [name = Jolin, age = 21], User [name = Jack Cheng, age = 22], User [name = Bruce Lee, age = 22], User [name = Jay, age = 30] After the sort method is called, the elements in the List are sorted by age.