package com.Lucky.Map; import java.util.Comparator; import java.util.TreeMap; /* TreeMap:底层结构和TreeSet一样是红黑树 可以排序/无重复/无索引 注意点:排序方法也是和TreeSet一样【自然排序/比较器排序】 */ public class TreeMapDemo { public static void main(String[] args) { /** * 要求1:键储存id,值储存商品 按照id升序排列【默认的】和降序排列【利用比较器排序】 */ TreeMap<Integer,String> Tmap=new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); Tmap.put(1,"联想拯救者"); Tmap.put(5,"外星人"); Tmap.put(9,"惠普"); Tmap.put(4,"华为"); System.out.println(Tmap); /** * 要求2:键储存学生对象,值储存种族 按照年龄排序/如果年龄一样就比较姓名字母排序 */ TreeMap<Student,String> Smap=new TreeMap<>(); Student str1=new Student("唯易",22); Student str2=new Student("珠峰",20); Student str3=new Student("信仰",22); Student str4=new Student("张老三",26); Smap.put(str1,"汉"); Smap.put(str2,"少数名族"); Smap.put(str3,"少数名族"); Smap.put(str4,"汉"); System.out.println(Smap); } }
材料:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Lucky.Map; 2 3 import java.util.Objects; 4 5 public class Student implements Comparable<Student>{ 6 private String name; 7 private int age; 8 9 public Student() { 10 } 11 12 public Student(String name, int age) { 13 this.name = name; 14 this.age = age; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public int getAge() { 26 return age; 27 } 28 29 public void setAge(int age) { 30 this.age = age; 31 } 32 33 @Override 34 public boolean equals(Object o) { 35 if (this == o) return true; 36 if (o == null || getClass() != o.getClass()) return false; 37 Student student = (Student) o; 38 return age == student.age && Objects.equals(name, student.name); 39 } 40 41 @Override 42 public int hashCode() { 43 return Objects.hash(name, age); 44 } 45 46 @Override 47 public String toString() { 48 return "Student{" + 49 "name='" + name + '\'' + 50 ", age=" + age + 51 '}'; 52 } 53 54 @Override 55 public int compareTo(Student o) { 56 //比较年龄 57 int res=this.getAge()-o.getAge(); 58 if(res==0){ 59 //年龄相等,就比较姓名 60 res = this.getName().compareTo(o.getName()); 61 } 62 return res; 63 } 64 }View Code
综合小练习:
package com.Lucky.Map; /* 统计字符串str="ACBBSABCBASSAABCCSAAACBCSAA"; 统计每一个字符出现的次数,按照从多到少排列 */ import java.util.StringJoiner; import java.util.TreeMap; import java.util.function.BiConsumer; /** * 统计个数:1.计数器思想 2.Map思想 * 应用场景:HashMap用于不用排序的结果 * TreeMap用于要进行排序的结果 */ public class TreeMapDemo1 { public static void main(String[] args) { String str="ACBBSABCBASSAABCCSAAACBCSAA"; //创建TreeMap对象 TreeMap<Character,Integer> tmap=new TreeMap<>(); for (int i = 0; i < str.length(); i++) { char c=str.charAt(i); if(tmap.containsKey(c)){ //获取次数 int sum=tmap.get(c); sum++; tmap.put(c,sum); }else { tmap.put(c,1); } } //遍历数据,格式 A(10)B(6)C(6)S(5) //方法1 StringBuilder StringBuilder builder=new StringBuilder(); tmap.forEach(new BiConsumer<Character, Integer>() { @Override public void accept(Character character, Integer integer) { builder.append(character).append("(").append(integer).append(")"); } }); System.out.println(builder); //方法2 StringJoiner System.out.println("---------StringJoiner----------"); StringJoiner joiner=new StringJoiner("","",""); tmap.forEach((key,val)->{ joiner.add(key+"").add("(").add(val+"").add(")"); }); System.out.println(joiner); } }
标签:Map,name,age,TreeMap,Student,集合,new,public From: https://www.cnblogs.com/Lucky-only/p/16975491.html