当前位置:网站首页>TreeSet after class exercises
TreeSet after class exercises
2022-04-23 03:57:00 【Breakfast loving Xiao Wang】
TreeSet At the bottom are red and black trees , There is order in storage . Here are two small exercises .
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(" The incoming data types are inconsistent !");
}
}
package exercise;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* Create five objects of this class , And put these objects in TreeSet Collection ,(TreeSet You need to use generics to define ) Set in the following two ways
* The elements in are sorted , And traverse the output :
* (1) send Employee Realization comparable Interface , And press name Sort
* (2) establish TreeSet Time passes in comparator object , Sort by birthday date
*
* @author
* @create 2022-04-20-18:16
*/
public class EmployeeTest {
// Question two : By birthday date
@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();
// Comparison year
int minusYear = b1.getYear() - b2.getYear();
if (minusYear!=0){
return minusYear;
}
// Comparison month
int minusMonth=b1.getMonth()-b2.getMonth();
if (minusMonth!=0){
return minusMonth;
}
// Comparison day
return b1.getDay()-b2.getDay();
}
// return 0;
throw new RuntimeException(" The incoming data type is incorrect !");
}
});
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());
}
}
// Question 1 : Use natural ordering
// @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());
// }
//
// }
}
版权声明
本文为[Breakfast loving Xiao Wang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230350589829.html
边栏推荐
- ROS series (IV): ROS communication mechanism series (1): topic communication
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- Paddlepaddle model to onnx
- 【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
- The difference between lists, tuples, dictionaries and collections
- 【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
- Openvino only supports Intel CPUs of generation 6 and above
- Express中间件①(中间件的使用)
- Express middleware ① (use of Middleware)
- 知乎有问题,谁来解答?
猜你喜欢
Retrieval question answering system baseline
Express middleware ① (use of Middleware)
Second kill all interval related problems
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
基于PHP的代步工具购物商城
Openvino only supports Intel CPUs of generation 6 and above
ROS series (IV): ROS communication mechanism series (1): topic communication
Thought of reducing Governance -- detailed summary of binary search
阿里云IoT流转到postgresql数据库方案
随机推荐
ROS series (I): rapid installation of ROS
Install PaddlePaddle on ARM
ROS series (III): introduction to ROS architecture
减治思想——二分查找详细总结
ROS series (IV): ROS communication mechanism series (1): topic communication
Identificateur, mot - clé, type de données
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Thought of reducing Governance -- detailed summary of binary search
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Express中间件①(中间件的使用)
ROS series (V): common commands in ROS
51 single chip microcomputer: D / a digital to analog conversion experiment
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
PolarMask is not in the models registry
Vs studio modifies C language scanf and other errors
Machine translation baseline
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
Let matlab2018b support the mex configuration of vs2019
STM32 advanced timer com event