Java自用集合练习题:TreeSet的自然排序与定制排序
Posted 王六六的IT日常
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java自用集合练习题:TreeSet的自然排序与定制排序相关的知识,希望对你有一定的参考价值。
定义一个 Employee 类。
该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;并为每一个属性定义 getter, setter 方法;并重写 toString 方法输出 name, age, birthday。
MyDate 类包含: private 成员变量 year,month,day;并为每一个属性定义 getter, setter 方法;
创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使 Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
Employee.java
使 Employee 实现 Comparable 接口,并按 name 排序
package com.atguigu.exer1;
/*
*
* 该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;
* 并为每一个属性定义 getter, setter 方法;
* 并重写 toString 方法输出 name, age, birthday
* @author WanZi
* @create 2021-09-24 11:45
*/
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);
}
throw new RuntimeException("传入的数据类型不一致");
}
}
MyDate.java
package com.atguigu.exer1;
/**
*
* private 成员变量 year,month,day;并为每一个属性定义 getter, setter方法;
* @author WanZi
* @create 2021-09-24 11:48
*/
public class MyDate implements Comparable{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public MyDate() {
}
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 +
'}';
}
@Override
public int compareTo(Object o) {
if(o instanceof MyDate){
MyDate m = (MyDate)o;
//比较年
int minusYear = this.getYear() - m.getYear();
if(minusYear != 0){
return minusYear;
}
//比较月
int minusMonth = this.getMonth() - m.getMonth();
if(minusMonth != 0){
return minusMonth;
}
//比较日
return this.getDay() - m.getDay();
}
throw new RuntimeException("传入的数据不一致!");
}
}
EmployeeTest.java (测试)
package com.atguigu.exer1;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
*
* 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中
*
* 分别按以下两种方式对集合中的元素进行排序,并遍历输出:
* 1). 使 Employee 实现 Comparable 接口,并按 name 排序
* 2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
*
* @author WanZi
* @create 2021-09-24 13:22
*/
public class EmployeeTest {
//问题2:定制排序 按生日日期的先后排序。
@Test
public void test2(){
TreeSet<Employee> set = new TreeSet<Employee>(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 b1.compareTo(b2);
}
throw new RuntimeException("传入的数据不一致!");
}
});
//添加数据
Employee e1 = new Employee("刘德华",55,new MyDate(1965,5,4));
Employee e2 = new Employee("张学友",43,new MyDate(1987,5,4));
Employee e3 = new Employee("郭富城",44,new MyDate(1987,5,9));
Employee e4 = new Employee("黎明",51,new MyDate(1954,8,12));
Employee e5 = new Employee("梁朝伟",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
//遍历
Iterator<Employee> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
//问题1: 按 name 排序 使用自然排序
@Test
public void test1(){
TreeSet<Employee> set = new TreeSet<Employee>();
Employee e1 = new Employee("刘德华",55,new MyDate(1965,5,4));
Employee e2 = new Employee("张学友",43,new MyDate(1987,5,4));
Employee e3 = new Employee("郭富城",44,new MyDate(1987,5,9));
Employee e4 = new Employee("黎明",51,new MyDate(1954,8,12));
Employee e5 = new Employee("梁朝伟",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
//遍历
Iterator<Employee> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
以上是关于Java自用集合练习题:TreeSet的自然排序与定制排序的主要内容,如果未能解决你的问题,请参考以下文章
Java学习笔记5.3.2 Set接口 - TreeSet类