示例:
示例公用类:
public class MyDate { private int month; private int year; private int day; @Override public String toString() { return "MyDate{" + "month=" + month + ", year=" + year + ", day=" + day + '}'; } public MyDate(int month, int year, int day) { this.month = month; this.year = year; 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; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } }
自然排序:在需要排序的属性的类中重写compareTo()方法,完成TreeSet集合按Employee的name属性排序:
public class Employee implements Comparable { private String name; private int age; private MyDate birthday; public Employee(String name, int age, MyDate birthday) { this.name = name; this.age = age; this.birthday = birthday; } public Employee() { } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", age=" + age + ", 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 int compareTo(Object o) { if(o instanceof Employee){ Employee e = (Employee)o; return this.name.compareTo(e.name); } return 0; } }
public class Test1 { public static void main(String args[]) { TreeSet tree = new TreeSet(); Employee employee1 = new Employee("yangshijie",20,new MyDate(2001,7,23)); Employee employee2 = new Employee("yanzhihang",18,new MyDate(2003,1,9)); Employee employee3 = new Employee("chenkangjia",18,new MyDate(2003,1,9)); Employee employee4 = new Employee("luoxingzhe",20,new MyDate(2002,7,23)); Employee employee5 = new Employee("wangwei",20,new MyDate(2001,8,23)); tree.add(employee1); tree.add(employee2); tree.add(employee3); tree.add(employee4); tree.add(employee5); Iterator iterator = tree.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
示例二:定制排序,创建comparator对象并并重写compare()方法,将创建的对象作为元素传入TreeSet的构造器中实现排序。
public class Test1 { public static void main(String args[]) { Comparator com = 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; if (e1.getBirthday().getYear()!=e2.getBirthday().getYear()){ return e1.getBirthday().getYear()-e2.getBirthday().getYear(); }else if(e1.getBirthday().getMonth()!=e2.getBirthday().getMonth()){ return e1.getBirthday().getMonth()-e2.getBirthday().getMonth(); }else{ return e1.getBirthday().getDay()-e2.getBirthday().getDay(); } } throw new RuntimeException("传入数据错误!"); } }; TreeSet tree = new TreeSet(com); Employee employee1 = new Employee("yangshijie",20,new MyDate(2001,7,23)); Employee employee2 = new Employee("yanzhihang",18,new MyDate(2003,1,9)); Employee employee3 = new Employee("chenkangjia",18,new MyDate(2003,1,9)); Employee employee4 = new Employee("luoxingzhe",20,new MyDate(2002,7,23)); Employee employee5 = new Employee("wangwei",20,new MyDate(2001,8,23)); tree.add(employee1); tree.add(employee2); tree.add(employee3); tree.add(employee4); tree.add(employee5); Iterator iterator = tree.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
public class Employee { private String name; private int age; private MyDate birthday; public Employee(String name, int age, MyDate birthday) { this.name = name; this.age = age; this.birthday = birthday; } public Employee() { } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", age=" + age + ", 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; } }