当前位置:网站首页>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
边栏推荐
- Installation and configuration of clion under win10
- 51 single chip microcomputer: D / a digital to analog conversion experiment
- (valid for personal testing) compilation guide of paddedetection on Jetson
- Paddlepaddle model to onnx
- 【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
- Hard core chip removal
- 现货黄金操作技巧_估波曲线
- 标识符、关键字、数据类型
- Paddlepaddle does not support arm64 architecture.
- Jupiter notebook modify configuration file setting startup directory is invalid
猜你喜欢

VSCode配置之Matlab极简配置

Network principle | connection management mechanism in TCP / IP important protocol and core mechanism

AI CC 2019 installation tutorial under win10 (super detailed - small white version)

抽象类、接口、常用关键字

LabVIEW 小端序和大端序区别

Use of rotary selector wheelpicker

Digital image processing third edition Gonzalez notes Chapter 2

【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解

Common auxiliary classes

将编译安装的mysql加入PATH环境变量
随机推荐
知乎有问题,谁来解答?
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
Man's life
Difference between LabVIEW small end sequence and large end sequence
Machine translation baseline
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
将编译安装的mysql加入PATH环境变量
小红书被曝整体裁员20%,大厂之间内卷也很严重
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
ROS series (IV): ROS communication mechanism series (3): parameter server
ROS series (V): common commands in ROS
Key point detection of human hand based on mediapipe
Express middleware ② (classification of Middleware)
ROS series (IV): ROS communication mechanism series (2): Service Communication
STM32 advanced timer com event
VS Studio 修改C语言scanf等报错
Shopping mall for transportation tools based on PHP
ROS series (IV): ROS communication mechanism series (6): parameter server operation
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
What if win10 doesn't have a local group policy?