TreeSet底层是红黑树,存放时有顺序。下面是两个小练习。
package exercise;
/**
* @author
* @create 2022-04-20-18:08
*/
public class MyDate {
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
package exercise;
/**
* @author
* @create 2022-04-20-18:10
*/
public class Employee implements Comparable{
private String name;
private int age;
private MyDate birthday;
public Employee() {
}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
@Override
public int compareTo(Object o) {
if (o instanceof Employee){
Employee e=(Employee) o;
return this.name.compareTo(e.name);
}
// return 0;
throw new RuntimeException("传入的数据类型不一致!");
}
}
package exercise;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 创建该类的五个对象,并把这些对象放入TreeSet集合中,(TreeSet需使用泛型来定义)分别按照以下两种方式对集合
* 中的元素进行排序,并遍历输出:
* (1)使Employee实现comparable接口,并按name排序
* (2)创建TreeSet时传入comparator对象,按生日日期的先后排序
*
* @author
* @create 2022-04-20-18:16
*/
public class EmployeeTest {
//问题二:按生日日期先后顺序
@Test
public void test2(){
TreeSet set =new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Employee && o2 instanceof Employee ){
Employee e1=(Employee) o1;
Employee e2=(Employee) o2;
MyDate b1 = e1.getBirthday();
MyDate b2 = e2.getBirthday();
//比较年
int minusYear = b1.getYear() - b2.getYear();
if (minusYear!=0){
return minusYear;
}
//比较月
int minusMonth=b1.getMonth()-b2.getMonth();
if (minusMonth!=0){
return minusMonth;
}
//比较日
return b1.getDay()-b2.getDay();
}
// return 0;
throw new RuntimeException("传入的数据类型有误!");
}
});
Employee e1=new Employee("LiuDeHua",55,new MyDate(1965,5,4));
Employee e2=new Employee("ZhangXueYou",43,new MyDate(1987,5,4));
Employee e3=new Employee("GuoFuCheng",44,new MyDate(1987,5,9));
Employee e4=new Employee("LiMing",51,new MyDate(1954,8,12));
Employee e5=new Employee("LiangChaoWei",21,new MyDate(1978,5,7));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
//问题一:使用自然排序
// @Test
// public void test1(){
// TreeSet set =new TreeSet();
//
// Employee e1=new Employee("LiuDeHua",55,new MyDate(1965,5,4));
// Employee e2=new Employee("ZhangXueYou",43,new MyDate(1987,5,4));
// Employee e3=new Employee("GuoFuCheng",44,new MyDate(1987,5,9));
// Employee e4=new Employee("LiMing",51,new MyDate(1954,8,12));
// Employee e5=new Employee("LiangChaoWei",21,new MyDate(1978,5,7));
// set.add(e1);
// set.add(e2);
// set.add(e3);
// set.add(e4);
// set.add(e5);
// Iterator iterator = set.iterator();
// while (iterator.hasNext()){
// System.out.println(iterator.next());
// }
//
// }
}