实现Comparable接口 按名字排序
@Test
public void test1 ( ) {
TreeSet set = new TreeSet ( ) ;
Employee e1 = new Employee ( "Chen" , new MyDate ( 1998 , 01 , 01 ) ) ;
Employee e2 = new Employee ( "Tom" , new MyDate ( 1997 , 2 , 01 ) ) ;
Employee e3 = new Employee ( "Tim" , new MyDate ( 2002 , 01 , 10 ) ) ;
Employee e4 = new Employee ( "Jane" , new MyDate ( 2001 , 2 , 01 ) ) ;
Employee e5 = new Employee ( "Mache" , new MyDate ( 2003 , 2 , 01 ) ) ;
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 ( ) ) ;
}
}
Employee类
public class Employee implements Comparable {
private static LocalDate date = LocalDate. now ( ) ;
private String name;
private int age;
private MyDate birthday;
public Employee ( String name, MyDate birthday) {
this . name = name;
this . birthday = birthday;
this . age = date. getYear ( ) - birthday. getYear ( ) ;
}
public String getName ( ) {
return name;
}
public int getAge ( ) {
return age;
}
@Override
public String toString ( ) {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}' ;
}
@Override
public int compareTo ( Object o) {
if ( o instanceof Employee ) {
int compare = this . name. compareTo ( ( ( Employee) o) . getName ( ) ) ;
if ( compare!= 0 ) {
return compare;
} else {
return Integer. compare ( this . age, ( ( Employee) o) . getAge ( ) ) ;
}
} else {
throw new RuntimeException ( "class don't same" ) ;
}
}
public MyDate getBirthday ( ) {
return birthday;
}
}
实现Comparator接口 按出生日期排序 或者实现Comparable·接口重写compareTo方法
@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 ) {
int compareYear = Integer. compare ( ( ( Employee) o1) . getBirthday ( ) . getYear ( ) , ( ( Employee) o2) . getBirthday ( ) . getYear ( ) ) ;
if ( compareYear != 0 ) {
return - compareYear;
} else {
int compareMonth = Integer. compare ( ( ( Employee) o1) . getBirthday ( ) . getMonth ( ) , ( ( Employee) o2) . getBirthday ( ) . getMonth ( ) ) ;
if ( compareMonth != 0 ) {
return - compareMonth;
} else {
return - Integer. compare ( ( ( Employee) o1) . getBirthday ( ) . getDay ( ) , ( ( Employee) o2) . getBirthday ( ) . getDay ( ) ) ;
}
}
} else {
throw new RuntimeException ( "type don't same!" ) ;
}
}
} ) ;
Employee e1 = new Employee ( "Chen" , new MyDate ( 1998 , 01 , 01 ) ) ;
Employee e2 = new Employee ( "Tom" , new MyDate ( 1997 , 2 , 01 ) ) ;
Employee e3 = new Employee ( "Tim" , new MyDate ( 2002 , 01 , 10 ) ) ;
Employee e4 = new Employee ( "Jane" , new MyDate ( 2001 , 2 , 01 ) ) ;
Employee e5 = new Employee ( "Mache" , new MyDate ( 2003 , 2 , 01 ) ) ;
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 ( ) ) ;
}
}
Employee 类
public class Employee {
private static LocalDate date = LocalDate. now ( ) ;
private String name;
private int age;
private MyDate birthday;
public Employee ( String name, MyDate birthday) {
this . name = name;
this . birthday = birthday;
this . age = date. getYear ( ) - birthday. getYear ( ) ;
}
public String getName ( ) {
return name;
}
public int getAge ( ) {
return age;
}
@Override
public String toString ( ) {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}' ;
}
public MyDate getBirthday ( ) {
return birthday;
}
}
Set面试题
@Test
public void test3 ( ) {
HashSet set = new HashSet ( ) ;
Person p1 = new Person ( 1001 , "AA" ) ;
Person p2 = new Person ( 1002 , "BB" ) ;
set. add ( p1) ;
set. add ( p2) ;
System. out. println ( set) ;
p1. name = "CC" ;
set. remove ( p1) ;
System. out. println ( set) ;
set. add ( new Person ( 1001 , "CC" ) ) ;
System. out. println ( set) ;
set. add ( new Person ( 1001 , "AA" ) ) ;
System. out. println ( set) ;
}