TreeMap的使用

1.案例需求

  • 创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历
  • 要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序
package com.ding.mymap;


public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public int compareTo(Student o) {

        //按照年龄排序
        int result = o.getAge() - this.getAge();

        //次要条件,按照姓名排序
        result = result == 0 ? o.getName().compareTo(this.getName()) : result;

        return result;

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.ding.mymap;


import java.util.TreeMap;

public class TreeMapTest {


    public static void main(String[] args) {
        TreeMap<Student, String> tm = new TreeMap<>();
        // 创建学生对象
        Student s1 = new Student("xiaohei",23);
        Student s2 = new Student("dapang",22);
        Student s3 = new Student("xiaomei",22);
        Student s4 = new Student("lisi",24);

        // 将学生对象添加到TreeMap集合中
        tm.put(s1,"江苏");
        tm.put(s2,"北京");
        tm.put(s3,"天津");
        tm.put(s4,"天津");

        tm.forEach(
                (Student key, String value) -> {
                    System.out.println(key + "---->" + value);
                }

        );
    }
   Set<Map.Entry<Student, String>> entries = tm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();

            System.out.println(key + "--->" + value);
        }

       /*
        Student{name='lisi', age=24}---->天津
        Student{name='xiaohei', age=23}---->江苏
        Student{name='xiaomei', age=22}---->天津
        Student{name='dapang', age=22}---->北京
        */


}

2.案例需求

  • 给定一个字符串,要求统计字符串中每个字符出现的次数。
  • 举例: 给定字符串是“aababcabcdabcde”,在控制台输出: “a(5)b(4)c(3)d(2)e(1)”
package com.ding.mymap;


import java.util.TreeMap;

public class TreeMapTest2 {
    public static void main(String[] args) {
        String s = "aababcabcdabcde";
        TreeMap<Character, Integer> tm = new TreeMap<>();

        for (int i = 0; i < s.length(); i++) {
            char c =s.charAt(i);
            if (!tm.containsKey(c)) {
                tm.put(c,1);
            } else {
                Integer count = tm.get(c);
                count++;
                tm.put(c, count);
            }
        }

        tm.forEach(
                (Character key, Integer value) -> {
                    System.out.println(key +"(" + value + ")");
                }
        );
    }
   
}

posted @ 2021-10-18 22:58  丁帅帅dss  阅读(299)  评论(0)    收藏  举报