1. 程式人生 > >TreeSet之定制排序和自然排序

TreeSet之定制排序和自然排序

his || ceo 自定義對象 總結 urn 接口 next test

TreeSet的幾大特點:
1、TreeSet中存儲的類型必須是一致的,不能一下存int,一下又存string
2、TreeSet在遍歷集合元素時,是有順序的【從小到大】(我的理解,如果存的字母,按字典序排列)
3、排序:當向TreeSet中添加自定義對象時,有2種排序方法,1:自然排序 2、定制排序
自然排序:要求自定義類實現java.lang.Comparable接口並重寫compareTo(Object obj)方法。在此方法中,指明按照自定義類的哪個屬性進行排序

一、自然排序示例:
1、定義一個類(文章中為Employee)實現Comparable接口
2、重寫Comparable接口中的compareTo()方法
3、在compareTo()中按指定屬性進行排序(文章按name進行排序)
代碼示例:
Employee類
public class Employee implements Comparable{
     public int compareTo(Object o) {
if (o instanceof Employee) {
Employee e = (Employee) o;
return this.name.compareTo(e.name);
}
return 0;
}

private String name;
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(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}

@Override
public String toString() {
return "Employee{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
", birthday=" + birthday +
‘}‘;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Employee employee = (Employee) o;

if (age != employee.age) return false;
if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
return birthday != null ? birthday.equals(employee.birthday) : employee.birthday == null;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
return result;
}
}

測試類
public class TreeSetTest {
public static void main(String[] args) {
test1();
}
/**
* 自然排序 按name排序 所以你的Employee必須實現Comparable接口
*/
public static void test1() {
Employee e1 = new Employee("liudehua",55,new MyDate(4,12,1997));
Employee e2 = new Employee("11",55,new MyDate(5,12,1997));
Employee e3 = new Employee("22",55,new MyDate(6,12,1997));
Employee e4 = new Employee("33",55,new MyDate(7,12,1997));
Employee e5 = new Employee("44",55,new MyDate(8,12,1997));
TreeSet set = new TreeSet();
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);

Iterator i = set.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
輸出結果按name進行排序。按照漢語拼音的順序

二、定制排序示例
Employee1 類
public class Employee1 {
    private String name;
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 Employee1(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}

@Override
public String toString() {
return "Employee{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
", birthday=" + birthday +
‘}‘;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Employee1 employee = (Employee1) o;

if (age != employee.age) return false;
if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
return birthday != null ? birthday.equals(employee.birthday) : employee.birthday == null;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
return result;
}
}

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

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

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

//先重寫MyDate的equals()和hashCode()方法,在重寫Employee中的

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

MyDate myDate = (MyDate) o;

if (day != myDate.day) return false;
if (month != myDate.month) return false;
return year == myDate.year;
}

@Override
public int hashCode() {
int result = day;
result = 31 * result + month;
result = 31 * result + year;
return result;
}
}

測試類
public class TreeSetTest1 {
public static void main(String[] args) {
test2();
}
/**
* 定制排序 按指定生日來排
*/
public static void test2() {
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 instanceof Employee1 && o2 instanceof Employee1) {
Employee1 e1 = (Employee1)o1;
Employee1 e2 = (Employee1)o2;

MyDate birth1 = e1.getBirthday();
MyDate birth2 = e2.getBirthday();

if(birth1.getYear() != birth2.getYear()) {
//定義的類型是 int 所以使用“-”減號代替compareTo()
return birth1.getYear() - birth2.getYear();
} else {
if (birth1.getMonth() != birth2.getMonth()) {
return birth1.getMonth() - birth2.getMonth();
} else {
return birth1.getDay() - birth2.getDay();
}
}
}
return 0;
}
};

// “一定要指明按特定對象進行比較 comparator參數一定要加”
TreeSet set = new TreeSet(comparator);
Employee1 e1 = new Employee1("liudehua",55,new MyDate(5,8,1990));
Employee1 e2 = new Employee1("11",55,new MyDate(5,11,1997));
Employee1 e3 = new Employee1("22",55,new MyDate(6,10,1997));
Employee1 e4 = new Employee1("33",55,new MyDate(7,9,1997));
Employee1 e5 = new Employee1("44",55,new MyDate(8,8,1997));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);

Iterator i = set.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}


總結:
自然排序實現的是comparable接口。其在類可以修改時使用。

定制排序實現的是comparator接口。其在類不可以修改時使用

在使用定制排序或是自然排序時,在其用到的類中都要重寫hashCode()與equals()方法


comparable和comparator的區別:[參考博文:http://blog.csdn.net/excellentyuxiao/article/details/52344594]
Comparator在util包下,Comparable在lang包下。java中的對象排序都是以comparable接口為標準的。comparator是在對象外部實現排序。


TreeSet之定制排序和自然排序