[javaSE] 集合框架(TreeSet)
TreeSet:可以对Set集合中的元素排序,默认按照ascii表排序,二叉树结构
左边叉是小的,右边叉是大的
存储自定义对象
定义一个类Student实现Comparable类,使自定义类具备比较性
定义属性年龄age
定义属性姓名name
实现compareTo()方法,传递进来另一个Student对象
判断当前Student对象的age大于另一个Student对象的age,返回1,否则返回-1
获取Student对对象
调用TreeSet对象的add()方法,参数:Student对象
遍历集合
import java.util.TreeSet; public class TreeSetDemo { /** * @param args */ public static void main(String[] args) { TreeSet<Student> treeset=new TreeSet<Student>(); treeset.add(new Student("taoshihan1",30)); treeset.add(new Student("taoshihan2",20)); treeset.add(new Student("taoshihan3",40)); for(Student student:treeset){ System.out.println(student.name+"==="+student.age); } } } class Student implements Comparable<Student>{ public int age; public String name; public Student(String name,int age) { this.name=name; this.age=age; } @Override public int compareTo(Student o) { if(o.age<this.age){ return 1; }else{ return -1; } } }
结果:
taoshihan2===20
taoshihan1===30
taoshihan3===40
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网
浙公网安备 33010602011771号