public class TreeMapDemo1 {
    public static void main(String[] args) {
        //integer,double 等默认情况都是升序排列
        //String 默认情况是按照首字母的升序排列
        TreeMap<Integer,String> tm1=new TreeMap<>(/*new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;//根据商品价格升序排列
            }
        }*/);
        tm1.put(1,"haha");
        tm1.put(3,"xiangxiang");
        tm1.put(2,"baby");
        System.out.println(tm1);
        TreeMap<Integer,String> tm2=new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//根据商品价格降序排列
            }
        });
        tm2.put(1,"haha");
        tm2.put(3,"xiangxiang");
        tm2.put(2,"baby");
        System.out.println(tm2);
        TreeMap<Student,String> tm3=new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i=o1.getAge()-o2.getAge();
                i=i==0?o1.getName().compareTo(o2.getName()):i;
                return i;//根据学生年龄升序,年龄相同,根据姓名升序
            }
        });
        Student s1=new Student("haha",18);
        Student s2=new Student("xiangxiang",17);
        Student s3=new Student("baby",19);
        tm3.put(s1,"haha");
        tm3.put(s2,"xiangxiang");
        tm3.put(s3,"baby");
        System.out.println(tm3);
    }
}
--------------------------------
public class TreeMapDemo2 {
    public static void main(String[] args) {
        /*
        统计字符串"abcbbdddccbacbddab"中每个字符出现的次数
         */
        //统计的类型太多,可以使用Map集合
        //键:表示字符串中的字符
        //值:表示字符出现的次数
        //如果要有序,可以使用TreeMap集合,无序则可以用HashMap集合
        String str="abcbbdddccbacbddab";
        TreeMap<Character,Integer> tm=new TreeMap<>();
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if(!tm.containsKey(c)){
                tm.put(c,1);
            }else{
                int count=tm.get(c);
                count++;
                tm.put(c,count);
            }
        }
        System.out.println(tm);
        tm.forEach((key,value)->{
            System.out.print(key+"("+value+") ");
        });//不建议这么写,不能得到结果只是打印出来了

        System.out.println();
        StringBuilder sb=new StringBuilder();
        tm.forEach((key,value)->{
            sb.append(key).append("(").append(value).append(")").append(" ");
        });
        System.out.println(sb);
        StringJoiner sj=new StringJoiner("","","");
        tm.forEach((key,value)->{
            sj.add(key+"("+value+") ");
        });
        System.out.println(sj);
    }
}