Map集合之TreeMap

TreeMap: 键不允许重复 底层是树的结构 可排序
TreeMap 如果将自定义类放在 key的位置 ,那这个类必须实现 自然排序或者 定制排序,否则报 ClassCastException
如何实现排序? 两种方式:
1 自然排序:
1> 创建需要排序的类 实现 Comparable <需要排序的类型>
2> 重写 compareTo 返回值如果返回0 证明两个对象相同,则不能存入集合
如果返回 1 -1 升序 降序
调用者比参数大 返回1 就是升序
调用者比参数小 返回1 就是降序
允许出现 第一条件...第二条件...
3> 创建TreeSet集合 将类放入 TreeSet集合的泛型中
2 定制排序:
1> 创建需要排序的类
2> 创建比较器的类 实现 Comparator <需要排序的类>
3> 重写 compare方法
参数 o1 类似于 compareTo方法中的this 也就是调用者
参数 o2 类似于 compareTo方法中的参数
4> 创建TreeSet集合 泛型< 需要排序的类> 构造方法中 必须传递 比较器对象
举个例子
自然排序

package com.qf.demo4;

import java.text.CollationKey;
import java.text.Collator;

public class Person implements Comparable<Person>{

    private String name;
    private int age;
    private String sex;
    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Person() {
        super();
    }
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }

    @Override
    public int compareTo(Person o) {
        // this  o 
        // 第一条件  比年龄  降序
        if(this.age > o.age){
            return -1;
        }else if(this.age < o.age){
            return 1;
        }else{
            // 第二条件 姓名  升序
            Collator collator = Collator.getInstance();
            CollationKey key = collator.getCollationKey(this.name);
            CollationKey key2 = collator.getCollationKey(o.name);
            return key.compareTo(key2);
            
        }
    
    }
}

Text.java

package com.qf.demo4;

import java.util.Comparator;
import java.util.TreeMap;
/**
 * TreeMap  自定义类 必须放在键的位置  , 自然排序 和定制排序 才能够起到作用
 *  
 */
public class Test {

    public static void main(String[] args) {
        
        TreeMap<String, String> map = new TreeMap<>();
        map.put("元芳", "睡吧");
        map.put("达康书记", "不能睡");
        map.put("皮皮虾", "能");
        System.out.println(map);
        
        TreeMap<Person, String> map2 = new TreeMap<>();
        map2.put(new Person("小乔", 18, "男"), "不可思议");
        map2.put(new Person("大乔", 18, "男"), "不可思议");
        map2.put(new Person("大乔", 18, "男"), "不可思议");
        System.out.println(map2);
        
        
        TreeMap<String, Person> map3 = new TreeMap<>();
        map3.put("hehe", new Person("程咬金", 1000, "男"));
        map3.put("haha", new Person("刘备", 2000, "男"));
        map3.put("xixi", new Person("刘备", 2000, "男"));
        System.out.println(map3);
        // 匿名内部类的形式 也可以帮助实现定制排序
        TreeMap<Person , String> map4 = new TreeMap<>(new Comparator<Person>() {

            @Override
            public int compare(Person o1, Person o2) {
            
                
                return 0;
            }
        });
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 以下是《疯狂Java讲义》中的一些知识,如有错误,烦请指正。 集合概述 Java集合可以分为Set、List、Ma...
    hainingwyx阅读 555评论 0 1
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 3.3 集合 一方面, 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储。另...
    闫子扬阅读 752评论 0 1
  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 1,091评论 0 4
  • 文 搬砖哥 《搬砖歌》 尚存一息不言输,砖头瓦粒铸路途。 放眼祖国两万里,多少乡客在外泊。 一日三餐求温饱,南来北...
    一枚搬砖哥阅读 1,834评论 42 31