TreeSet
程序员文章站
2022-05-14 13:06:30
...
特征
1、底层实现是TreeMap, 数据结构是二叉树
2、不保证顺序,但是可以自定义排序、唯一,可为null
3、线程不同步,多线程使用不安全
4、TreeSet集合只能存储可排序的对象
5、如何实现排序
- 让元素具有排序功能
元素类实现Comparable接口,重写CompareTo方法
1.在元素被添加进容器时,会调用compareTo方法和已经存在的元素进行比较
2.如果返回的int值是正数,这个元素会排在被比较元素的后面
3.如果返回的int值是负数,这个元素会排在被比较元素的前面
4.如果返回的int值是0,表示无法比较,这个元素无法添加进容器- 让容器具有排序功能
1.创建一个比较器类,实现Comparator,重写compare方法
2.使用有参构造创建TreeSet,参数就是比较器类的对象
测试代码
public class Demo02TreeSet {
@Test
public void test01(){
TreeSet<String> set = new TreeSet<>();
set.add("王子玉");
set.add("韩文龙");
set.add("任玉文");
System.out.println(set);
}
@Test
public void test02(){
TreeSet<Student> set = new TreeSet<>();
set.add(new Student("任玉文", 20, '男'));
set.add(new Student("韩文龙", 30, '女'));
set.add(new Student("钟雪", 18, '女'));
System.out.println(set);
}
@Test
public void test03(){
TeacherComparator teacherComparator = new TeacherComparator();
TreeSet<Teacher> set = new TreeSet<>(teacherComparator);
set.add(new Teacher("任玉文", 20, '男'));
set.add(new Teacher("韩文龙", 30, '女'));
set.add(new Teacher("钟雪", 18, '女'));
System.out.println(set);
}
@Test
public void test04(){
TreeSet<Girlfriend> set = new TreeSet<>();
set.add(new Girlfriend("a", 90, 20));
set.add(new Girlfriend("b", 90, 21));
set.add(new Girlfriend("c", 95, 20));
set.add(new Girlfriend("d", 95, 21));
System.out.println(set);
}
@Test
public void test05(){
GirlfriendComparator girlfriendComparator = new GirlfriendComparator();
TreeSet<Girlfriend> set = new TreeSet<>(girlfriendComparator);
set.add(new Girlfriend("a", 90, 20));
set.add(new Girlfriend("b", 90, 21));
set.add(new Girlfriend("c", 95, 20));
set.add(new Girlfriend("d", 95, 21));
System.out.println(set);
}
@Test
public void test06(){
Comparator<Girlfriend> comparator = (Girlfriend o1, Girlfriend o2) -> {
if (o2.getScore() != o1.getScore()){
return o2.getScore() > o1.getScore() ? 1 : -1;
}else if (o2.getScore() == o1.getScore()) {
return o1.getAge() - o2.getAge();
}else if (o2.getAge() == o1.getAge()){
return 1;
} else {
return 1;
}
};
TreeSet<Girlfriend> set = new TreeSet<>(comparator);
set.add(new Girlfriend("a", 90, 20));
set.add(new Girlfriend("b", 90, 21));
set.add(new Girlfriend("c", 95, 20));
set.add(new Girlfriend("d", 95, 21));
System.out.println(set);
}
}
package org.jgs1904.comparators;
import org.jgs1904.entity.Teacher;
import java.util.Comparator;
/**
* @ClassName TeacherComparator
* @Description TODO
* @Author RenYuWen
* @Since 2019/12/5 10:06
*/
public class TeacherComparator implements Comparator<Teacher> {
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.getAge() - o2.getAge();
}
}
package org.jgs1904.comparators;
import org.jgs1904.entity.Girlfriend;
import java.util.Comparator;
/**
* @ClassName GirlfriendComparator
* @Description TODO
* @Author RenYuWen
* @Since 2019/12/5 10:40
*/
public class GirlfriendComparator implements Comparator<Girlfriend> {
@Override
public int compare(Girlfriend o1, Girlfriend o2) {
if (o2.getScore() != o1.getScore()){
return o2.getScore() > o1.getScore() ? 1 : -1;
}else if (o1.getAge() != o2.getAge()) {
return o1.getAge() - o2.getAge();
} else {
return 1;
}
}
}
package org.jgs1904.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName Girlfriend
* @Description TODO
* @Author RenYuWen
* @Since 2019/12/5 10:17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Girlfriend implements Comparable<Girlfriend>{
private String name;
private int score;
private int age;
@Override
public int compareTo(Girlfriend girlfriend) {
if (girlfriend.getScore() != this.score){
return girlfriend.getScore() > this.score ? 1 : -1;
}else if (girlfriend.getScore() == this.score) {
return this.getAge() - girlfriend.getAge();
}else if (girlfriend.getAge() == this.getAge()){
return 1;
} else {
return 1;
}
}
}
package org.jgs1904.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName Teacher
* @Description TODO
* @Author RenYuWen
* @Since 2019/12/5 10:00
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
private String name;
private int age;
private char sex;
}
上一篇: php拾遗: 类型约束
推荐阅读