您的位置:首页 > 其它

TreeSet的排序两种实现方式Comparator和Comparable

2016-05-21 17:10 381 查看
TreeSet
条件

类型一样

设计到排序
comparable是自然(可以修改类的情况下)
comparator是定制(不可以修改类的情况下)优先级更高
 使用说明:
一般采用的是自然排序,但是当,不能对类进行修改时不得不采用comparator方法,下面的demo采用了两种方式结合。
代码整体说明:
Employee类的birthday属性是自定义类MyDate类型
Employee采用comparable方式默认的是按着姓名进行排序
然后采用定制的Comparator方式对出生日期进行排序package TestContainer;

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;
}

@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;
}

public void setYear(int year) {
this.year = year;
}

public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}

public MyDate() {
super();
}

@Override
public String toString() {
return "MyDate [day=" + day + ", month=" + month + ", year=" + year + "]";
}

}

package TestContainer;
//是否重写compareTo()和equals()方法
//虽然比较的是String或者Integer的compareTo()方法,但是,把Employee放到TreeSet中还需要判断多个Employee是否相等
public class Employee implements Comparable{
private String name;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((birthday == null) ? 0 : birthday.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 (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

private int age;
private MyDate 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;
}

public Employee() {
super();
}

public Employee(String name, int age, MyDate birthday) {
super();
this.name = name;
this.age = age;
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 employee=(Employee)o;
int i=this.getName().compareTo(employee.getName());
//			if(i==0){
//				int j=this.getAge()-employee.getAge();
////				if (j==0){
////
////				}
//				return j;
//			}
return i;
}
return 0;
}
}

package TestContainer;

import static org.junit.Assert.*;

import java.util.Comparator;
import java.util.TreeSet;

import org.junit.Test;

public class TestEmployee {

// 自然排序,Employee实现Comparable接口,并按着Name排序
@Test
public void test() {
Employee e1 = new Employee("Haid", 31, new MyDate(2, 2, 1985));
Employee e2 = new Employee("Qiao", 25, new MyDate(13, 10, 1991));
Employee e3 = new Employee("Alex", 20, new MyDate(2, 2, 1996));
Employee e4 = new Employee("Tom", 37, new MyDate(2, 2, 1980));
Employee e5 = new Employee("Alex", 28, new MyDate(13, 10, 1989));

// 定制排序Comparator
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 instanceof Employee && o2 instanceof Employee) {
Employee e1 = (Employee) o1;
Employee e2 = (Employee) o2;
int i = e1.getBirthday().getYear() - e2.getBirthday().getYear();
if (i == 0) {
int j = e1.getBirthday().getMonth() - e2.getBirthday().getMonth();
if (j == 0) {
return e1.getBirthday().getDay() - e2.getBirthday().getDay();
}
return j;
}
return i;
}
return 0;

}

};

TreeSet set = new TreeSet(comparator);
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);

for (Object object : set)
System.out.println(object);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: