Map container-TreeMap and common APIs, Comparator and Comparable interfaces, and treemapcomparator
TreeMap and common APIs
① The TreeMap class implements the Map interface by using the red/black tree;
② TreeMap provides an effective means to store key/value pairs in order, and allows quick retrieval;
③ Unlike HashMap, tree ing ensures that its elements are sorted in ascending order by keywords;
④ TreeMap constructor:
A) TreeMap ()
B) TreeMap (Comparator comp)
C) TreeMap (Map m)
D) TreeMap (SortedMap sm)
⑤ TreeMap implements SortedMap and extends AbstractMap. It does not define other methods;
1 TreeMap <String, String> tmap = new TreeMap <String, String> (); 2 tmap. put ("zhang", "zhang San"); 3 tmap. put ("jack", "James"); 4 tmap. put ("mary", "Xiaohong"); 5 tmap. put ("free", "lobular"); 6 tmap. put ("mary", "Grass"); 7 // tmap. put (null, "Grass"); // The key cannot be null, and an exception 8 System is thrown. out. println (tmap); 9
By default, the output is in ascending order of keys.
Output result:
{Free = Xiaoye, jack = James, mary = xiaocao, zhang = James}
Output all key-value pairs
1 Set<Entry<String,String>> entrys=tmap.entrySet();2 for(Entry<String,String> entry:entrys){3 System.out.println(entry.getKey()+"--"+entry.getValue());4 }
Output result:
Free -- Xiaoye
Jack -- James
Mary -- xiaocao
Zhang -- zhang San
Compile a Person class
1 class Person{ 2 private String name; 3 private int age; 4 public Person(String name, int age) { 5 super(); 6 this.name = name; 7 this.age = age; 8 } 9 public String getName() {10 return name;11 }12 public void setName(String name) {13 this.name = name;14 }15 public int getAge() {16 return age;17 }18 public void setAge(int age) {19 this.age = age;20 }21 }
Create and add elements in the main method, and output
1 TreeMap <Person, String> pdata = new TreeMap <Person, String> (); 2 pdata. put (new Person ("zhangsan", 20), "Zhang San"); 3 pdata. put (new Person ("lisi", 25), "Li Si"); 4 pdata. put (new Person ("wangwu", 30), "Wang Wu"); 5 pdata. put (new Person ("zhangsan", 33), "Zhang San"); 6 System. out. println (pdata );
Running result:
Error because the Person class is not implementedcompareTo
(T o)
Comparison Method
Method 1: implement the Comparable Interface
1 class Person implements Comparable <Person> {2 3}
The rewrite method is as follows: sort by age
1 @Override2 public int compareTo(Person o) {3 if (this.age - o.getAge() > 0) {4 return 1;5 } else if (this.age - o.getAge() < 0) {6 return -1;7 }8 return 0;9 }
Then execute the command and output the result:
{Com. iotek. map. person @ 2a139a55 = James, com. iotek. map. person @ 15db9742 = Li Si, com. iotek. map. person @ 6d06d69c = Wang Wu, com. iotek. map. person @ 7852e922 = James 3}
The complete Person class is as follows:
1 class Person implements Comparable <Person> {2 private String name; 3 private int age; 4 5 public Person (String name, int age) {6 super (); 7 this. name = name; 8 this. age = age; 9} 10 11 public String getName () {12 return name; 13} 14 15 public void setName (String name) {16 this. name = name; 17} 18 19 public int getAge () {20 return age; 21} 22 23 public void setAge (int age) {24 this. age = age; 25} 26 27 @ Override28 public int compareTo (Person o) {29 if (this. age-o. getAge ()> 0) {30 return 1; 31} else if (this. age-o. getAge () <0) {32 return-1; 33} 34 return 0; 35} 36}View Code
Method 2: Do not use the Comparable interface in Person. Use the anonymous internal class in the main method:
1 TreeMap<Person, String> pdata = new TreeMap<Person, String>(new Comparator<Person>() { 2 3 @Override 4 public int compare(Person o1, Person o2) { 5 if(o1.getAge()>o2.getAge()){ 6 return 1; 7 } 8 else if(o1.getAge()<o2.getAge()){ 9 return -1;10 }11 return 0;12 }13 });
The output result is the same as the preceding one.
The preceding steps use int age for sorting. The String name sorting method is as follows:
1 @Override2 public int compare(Person o1, Person o2) {3 return o1.getName().compareTo(o2.getName());4 }
Output result:
{Com. iotek. map. Person @ 2a139a55 = Li Si, com. iotek. map. Person @ 15db9742 = Wang Wu, com. iotek. map. Person @ 6d06d69c = Zhang San}
Because zhangsan is the same, it is replaced. The modification is as follows:
1 @ Override 2 public int compare (Person o1, Person o2) {3 if (o1.getName (). compareTo (o2.getName ()> 0) {4 return 1; 5} 6 else if (o1.getName (). compareTo (o2.getName () <0) {7 return-1; 8} 9 else {10 // The same age will still replace 11 return o1.getAge ()-o2.getAge (); 12} 13}
Comparator and Comparable Interfaces
① The key of TreeMap stores reference data and must meet certain conditions.
A) either reference type to implement the Comparable Interface
B) either provide a Comparator object for the TreeMap container to implement the Comparator Interface