当前位置:网站首页>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
边栏推荐
- 伦敦银最新价格走势图与买卖点
- VS Studio 修改C语言scanf等报错
- SQL learning record
- [AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
- Seekbar custom style details
- As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
- Digital image processing third edition Gonzalez notes Chapter 2
- Openvino only supports Intel CPUs of generation 6 and above
- Hard core chip removal
- matlab读取多张fig图然后合并为一张图(子图的形式)
猜你喜欢
LabVIEW 小端序和大端序区别
Hard core chip removal
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
標識符、關鍵字、數據類型
A function second kill 2sum 3sum 4sum problem
硬核拆芯片
[BIM introduction practice] wall hierarchy and FAQ in Revit
[latex] formula group
对象和类的概念
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
随机推荐
What if you encounter symbols you don't know in mathematical formulas
Machine translation baseline
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
Activity supports multi window display
創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
Abstract classes, interfaces and common keywords
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
Mysql出现2013 Lost connection to MySQL server during query
ROS series (4): ROS communication mechanism series (4): topic communication practice
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Instructions for fastmock
vscode删除卸载残余
RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
[mathematical modeling] my mathematical memory
Operating skills of spot gold_ Wave estimation curve
Difference between LabVIEW small end sequence and large end sequence