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 + "]";
}
}
条件
类型一样
设计到排序
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); } }
相关文章推荐
- Win7上帝模式
- CMake与Make最简单直接的理解
- android模拟器无法使用camera拍照
- UVa 213 - Message Decoding
- 单例模式—java实现
- (结构型模式)PROXY——代理模式
- Android SDK 在线更新镜像服务器资源
- 依赖倒转原则——里氏代换原则
- iOS Provisioning Profile(Certificate)与Code Signing详解
- JDK源码学习(6)-ConcurrentHashMap代码学习
- 开放——封闭原则(OCP)
- iOS 推送通知
- 最小生成树
- 1081 线段树练习 2
- 清除Eclipse和Myeclipse中的工作空间目录
- MySQL 相关
- 单一职责原则(SRP)
- s5pv210 datasheet_system_CONNECTIVITY/ STORAGE
- 利用VisuAlgo通过动画学习算法和数据结构
- (创建型模式)FACTORY——简单工厂模式