TreeMap

认识TreeMap

 TreeMap应用

需求1: 创建集合时使用比较器对象
package com.chen.集合.bao3;

import java.util.Comparator;
import java.util.TreeMap;

public class Demo {
    public static void main(String[] args) {
        
        //降序
        TreeMap<Integer,String > treeMap=new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

        treeMap.put(3,"张三");
        treeMap.put(2,"李四");
        treeMap.put(1,"王五");

        //默认升序
        System.out.println(treeMap);

    }
}

需求2:自定义对象实现Comparable
package com.chen.集合.bao3;

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

    public Student() {
    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

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

    @Override
    public int compareTo(Student o) {
        //按年龄升序排列,年龄一样,按姓名的字母排列
        int i = this.getAge() - o.getAge();
        //i<0 降序
        //i>0 升序
        //i=0 相等
        if(i==0){
            i=this.getName().compareTo(o.getName());
            //abcdefg...
        }

        return i;

    }
}
package com.chen.集合.bao3;

import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Student, String> treeMap = new TreeMap<>();

        Student student1 = new Student("张三", 23);
        Student student2 = new Student("李四", 12);
        Student student3 = new Student("王五", 39);

        treeMap.put(student1,"江苏");
        treeMap.put(student2,"上海");
        treeMap.put(student3,"北京");


        System.out.println(treeMap);


    }
}
需求3:统计字母个数

package com.chen.集合.bao3;

import java.util.TreeMap;

public class Demo1 {
    public static void main(String[] args) {

        //HashMap:结果不需要排序
        //TreeMap:结果需要排序
        TreeMap<Character, Integer> map = new TreeMap<>();

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

        System.out.println(map);
        map.forEach((k,v)->{
            System.out.print(k+"("+v+")");
        });
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值