当前位置:网站首页>TreeSet课后练习
TreeSet课后练习
2022-04-23 03:51:00 【爱吃早饭的小王】
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());
// }
//
// }
}
版权声明
本文为[爱吃早饭的小王]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_55895641/article/details/124305004
边栏推荐
- Installation and configuration of MinGW under win10
- VS Studio 修改C語言scanf等報錯
- What to pay attention to when writing the first code
- Activity supports multi window display
- The art of concurrent programming (6): explain the principle of reentrantlock in detail
- Leetcode 617 merge binary tree
- Definition format of array
- Oracle JDK vs OpenJDK
- ROS series (V): common commands in ROS
- PolarMask is not in the models registry
猜你喜欢
Common auxiliary classes
STM32 advanced timer com event
Summary of knowledge map (3)
Common net HP UNIX system FTP server listfiles returns null solution.
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
How Zotero quotes in word jump to references / hyperlink
What if you encounter symbols you don't know in mathematical formulas
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
Paddlepaddle does not support arm64 architecture.
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
随机推荐
Vs Studio modifie le langage C scanf et d'autres erreurs
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
The super large image labels in remote sensing data set are cut into specified sizes and saved into coco data set - target detection
Design and implementation of redis (2): how to handle expired keys
现货黄金操作技巧_估波曲线
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Identifier, keyword, data type
标识符、关键字、数据类型
Download and configuration of idea
Summary of knowledge map (I)
[mathematical modeling] my mathematical memory
作为一名码农,女友比自己更能码是一种什么体验?
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Software testing process
网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
Basic usage of Google colab (I)
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..