TreeSet联系 自然排序

******************************************************

创建需要的两个类

package com.hu.treeset;

public class MyDate {
private int day;
private int month;
private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "MyDate [day=" + day + ", month=" + month + ", year=" + year
+ "]";
}
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate other = (MyDate) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}

}

**********************************************************

package com.hu.treeset;

public class Employee implements Comparable{
private String name;
private int age;
private MyDate birthDate;
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 getBirthDate() {
return birthDate;
}
public void setBirthDate(MyDate birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", birthDate="
+ birthDate + "]";
}
public Employee(String name, int age, MyDate birthDate) {
super();
this.name = name;
this.age = age;
this.birthDate = birthDate;
}
@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e=(Employee) o;
return this.name.compareTo(e.name);
}
return 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((birthDate == null) ? 0 : birthDate.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (birthDate == null) {
if (other.birthDate != null)
return false;
} else if (!birthDate.equals(other.birthDate))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}

***************************************************

Treeset调用

package com.hu.treeset;

import static org.junit.Assert.*;

import java.util.Iterator;

import org.junit.Test;

public class TreeSet {

//用自然排序
@Test
public void test() {
Employee e1=new Employee("MuYongyuan", 12, new MyDate(04, 11, 2001));
Employee e2=new Employee("ZhouYiming", 12, new MyDate(07, 10, 2002));
Employee e3=new Employee("YuXi",9, new MyDate(04, 11, 2004));
Employee e4=new Employee("Wanghaoyu ", 15, new MyDate(04, 11, 2000));
Employee e5=new Employee("zhanganyue", 12, new MyDate(04, 11, 2003));

java.util.TreeSet set=new java.util.TreeSet();
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 test() {
Employee e1=new Employee("MuYongyuan", 12, new MyDate(04, 11, 2001));
Employee e2=new Employee("ZhouYiming", 12, new MyDate(07, 10, 2002));
Employee e3=new Employee("YuXi",9, new MyDate(04, 11, 2004));
Employee e4=new Employee("Wanghaoyu ", 15, new MyDate(04, 11, 2000));
Employee e5=new Employee("zhanganyue", 12, new MyDate(04, 11, 2003));

java.util.TreeSet set=new java.util.TreeSet();
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 test2(){

Comparator comparator=new Comparator() {

@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Employee1 && o2 instanceof Employee1){
Employee1 e1=(Employee1)o1;
Employee1 e2=(Employee1)o2;

MyDate bir1=e1.getBirthDate();
MyDate bir2=e2.getBirthDate();
if(bir1.getYear()!=bir2.getYear()){
return bir1.getYear()-bir2.getYear();

}else{
if(bir1.getMonth()!=bir2.getMonth()){
return bir1.getMonth()-bir2.getMonth();
}
else{
return bir1.getDay()-bir2.getDay();
}

}
}
return 0;
}
};
java.util.TreeSet set=new java.util.TreeSet(comparator);

Employee1 e1=new Employee1("MuYongyuan", 12, new MyDate(04, 10, 2002));
Employee1 e2=new Employee1("ZhouYiming", 12, new MyDate(07, 10, 2002));
Employee1 e3=new Employee1("YuXi",9, new MyDate(04, 11, 2004));
Employee1 e4=new Employee1("Wanghaoyu ", 15, new MyDate(04, 11, 2000));
Employee1 e5=new Employee1("zhanganyue", 12, new MyDate(04, 11, 2003));

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://www.cnblogs.com/afterhours/p/6136134.html