TreeSet 是一个可以对元素进行排序的容器。底层实际是用 TreeMap 实现的,内部维持了一个简化版的 TreeMap,通过 key 来存储 Set 的元素。 TreeSet 内部需要对存储的元素进行排序,因此,我们需要给定排序规则。
排序规则实现方式:通过元素自身实现比较规则。通过比较器指定比较规则。
1、通过元素自身实现比较规则
在元素自身实现比较规则时,需要实现 Comparable 接口中的 compareTo 方法,该方法中用来定义比较规则。
import java.util.Objects;
/**
* 测试通过元素自身比较规则比较
*/
public class Users implements Comparable<Users>{
private String username;
private int userage;
public Users(String username, int userage) {
this.username = username;
this.userage = userage;
}
public Users() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Users users = (Users) o;
return userage == users.userage && Objects.equals(username, users.username);
}
@Override
public int hashCode() {
return Objects.hash(username, userage);
}
@Override
public String toString() {
return "Users{" +
"username='" + username + '\'' +
", userage=" + userage +
'}';
}
//元素自身比较规则
@Override
public int compareTo(Users o) {
if (this.userage>o.getUserage()){
return 1;
}
if (this.userage==o.getUserage()){
return this.username.compareTo(o.getUsername());
}
return -1;
}
}
import java.util.Set;
import java.util.TreeSet;
/**
* 实现元素自身比较规则
*/
public class TreeSetTest1 {
public static void main(String[] args) {
//实例化
Set<Users> set = new TreeSet<>();
Users u = new Users("cwy",19);
Users u1 = new Users("whd",15);
Users u2 = new Users("by",19);
//添加元素
set.add(u);
set.add(u1);
set.add(u2);
for (Users temp:set){
System.out.println(temp);
}
2、通过比较器指定比较规则
通过比较器定义比较规则时,我们需要单独创建一个比较器,比较器需要实现Comparator 接口中的 compare 方法来定义比较规则。在实例化 TreeSet 时将比较器对象交给TreeSet 来完成元素的排序处理。此时元素自身就不需要实现比较规则了。
import java.util.Objects;
public class 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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 测试通过比较器实现比较规则
*/
import java.util.Comparator;
public class StudentComparator implements Comparator<Student> {
//定义比较器
@Override
public int compare(Student o1, Student o2) {
if (o1.getAge() > o2.getAge()){ //由小到大排序
return 1;
}
if (o1.getAge() == o2.getAge()){
return o1.getName().compareTo(o2.getName());
}
return -1;
}
}
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest1 {
public static void main(String[] args) {
//测试通过外部比较器实现比较规则
//实例化TreeSet需要给定比较器对象
Set<Student> set1 = new TreeSet<>(new StudentComparator());
Student s = new Student("cwy",18);
Student s1 = new Student("by",20);
Student s2 = new Student("whd",18);
Student s3 = new Student("by",19);
set1.add(s);
set1.add(s1);
set1.add(s2);
set1.add(s3);
for (Student temp:set1){
System.out.println(temp);
}
}
}