单列集合Set的实现类TreeSet
Set接口 [Collection】的子类
TreeSet
a.特点【无序,不可重复,查询快,可自动排序】,但需要指定排序规则,API中有一些类已经实现了Comparable接口],给出了默认排序规则,如:Integer:数值大小[升序] String:字典顺序等
b.指定排序规则方法:
一. 重写自然排序方法[compareTo]:
在对象类中实现implements Comparable接口<>
public class Demo1 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<>();//8位二进制数为一个字节,一个中文两个字节//unicode值ts.add(new Student("zhangsan", 20));ts.add(new Student("lisi", 21));ts.add(new Student("wangwu", 22));ts.add(new Student("zhaoliu", 22));ts.add(new Student("zhaoqi", 22));for (Student student : ts) {System.out.println(student);}}
}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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}//自定义比较器时不用重写,调用父类的即可@Overridepublic int compareTo(Student o) {//主要条件比较年龄int result = this.getAge() - o.getAge();//z = (x > y) ? x : y;//年龄相同比较名字的英文顺序result= result ==0 ? this.getName().compareTo(o.getName()) :result;return result;//也可用if语句/*if(result==0){result=this.getName().compareTo(o.getName());}*/}
}打印结果:
----------------------------------------------------------
Student{name='zhangsan', age=20}
Student{name='lisi', age=21}
Student{name='wangwu', age=22}
Student{name='zhaoliu', age=22}
Student{name='zhaoqi', age=22}
**二.**自定义比较器:
在创建容器时使用有参构造,加入比接口对象new Comparator()
//采用匿名内部类或者lambda表达式重写compare方法
public class Demo2 {public static void main(String[] args) {//TreeSet<Student> ts = new TreeSet<>();TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {@Override//compare返回值为int类型,如果计算的类型不同则需要强转public int compare(Student1 o1, Student1 o2) {//主要条件int result = o1.getAge() - o2.getAge();//次要条件result=result==0?o1.getName().compareTo(o2.getName()):result;return result;}});//lambda表达式/*TreeSet<Student> ts = new TreeSet<>((o1, o2) -> {//主要条件 对年龄排序int result = o1.getAge() - o2.getAge();//次要条件 年龄相同,对名字排序result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;return result;});*/ts.add(new Student("zhangsan", 20));ts.add(new Student("lisi", 21));ts.add(new Student("wangwu", 22));ts.add(new Student("zhaoliu", 22));ts.add(new Student("zhaoqi", 22));for (Student student : ts) {System.out.println(student);}}
}打印结果:
------------------------------------------------------------
Student{name='zhangsan', age=20}
Student{name='lisi', age=21}
Student{name='wangwu', age=22}
Student{name='zhaoliu', age=22}
Student{name='zhaoqi', age=22}
**三.**TreeSet底层实现思想【红黑树】
因为红黑树是有序的,所有TreeSet有排序功能
单列集合Set的实现类TreeSet相关推荐
- 单列集合List的实现类
List接口 [Collection]的子类 1.ArrayList[查询快,增删慢] (1)ArrayList常见方法[部分] //list[Collection的子类,也是接口],ArrayLis ...
- 单列集合Set的实现类HashSet
Set接口 [Collection]的子类 HashSet 特点[无序,不可重复,不能排序] 默认比较地址值[地址相同的值相同],重写后可比较内容[内容相同的值相同] 1.比较地址值[默认] publ ...
- JAVA day18,19 单列集合Collection:List(ArrayList,LinkedList,Vector)、Queue(Deque)、Set(HashSet,TreeSet),比较器
Collection单列集合 所有超级接口: Iterable<E> 所有已知子接口: BeanContext, BeanContextServices, BlockingDeque< ...
- 集合包括两大类:collection(单列集合祖宗接口)、Map(双列集合祖宗接口)
Collection:List.Set List集合添加的元素有序.重复.有索引 Set集合添加的元素无序.不重复.无索引 标题Collection常见的成员方法 add.clear.remove.c ...
- java中单列集合的根接口是_java 单列集合总结
Collection 接口 add() remove() contains() clear(); size(); 迭代器遍历(普通迭代器,不能再遍历过程中修改集合的长度) List接口 单列集合 有序 ...
- Java-Collection集合之单列集合List,以及遍历方式
Collection值单列集合List(),以及遍历方式 知识点-- 单列集合常用类的继承体系 讲解: Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口 ...
- Java---Collection单列集合详解
目录 一.单列集合的介绍 二.单列集合的使用 1:关于Collection (1)迭代器遍历 (2)增强for循环遍历 (3)Lambda表达式遍历 2:List的使用 (1)ArrayList的使用 ...
- 【Java源码分析】集合框架-Collections工具类-Arrays工具类
集合框架Collection 和Collections和Arrays工具类 一开始总是搞不清楚Collection和Collections的区别,不过看下代码应该很清楚,一个是接口,一个是工具类 Co ...
- Java集合迭代器原理图解_Java Iterator接口遍历单列集合迭代器原理详解
这篇文章主要介绍了Java Iterator接口遍历单列集合迭代器原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Iterator接口概述 ...
最新文章
- admui 能再php上用吗,Javascript 方法
- android 继承dialog自定义对话框
- CGAL window 10安装、Demo使用步骤以及问题解决记录
- SAP CDS view生成的OData服务对CRUD(增删改查)的支持
- 一场全能的开发者大会,来自助力开发者成功进阶的华为云
- 【Visio】 windows Visio 画图
- OPencv 灰度直方图、直方图规定化
- 计算机打印字与印刷字的大小,[转载]实际大小、实际印刷大小 字号 磅数 尺寸对照表及图片...
- Android Studio 如何快速把一个类中的所有同一个变量名统一改(同时工程项目里的文件名被改)
- java 设置时区_Java Calendar如何设置时区
- 1156: 单数变复数
- 回测系统backtrader(1)参考模板
- 澳大利亚域名_AU域名,澳大利亚域名,注册AU域名,au域名注册
- selenium登录某宝跳过反爬监测及滑块验证
- “狗屁不通文章生成器”项目登顶GitHub热榜~
- centos7 Kafka安装
- 互联网大佬做公益,图什么?
- MATLAB地图作为底图,matlab画图——地图背景
- 保姆级教程:基于Docker部署雨中冒险2服务器 雨中冒险2开服
- 在调试器里看LINUX内核态栈溢出