1. 程式人生 > >TreeSet舉例自然排序和定製排序

TreeSet舉例自然排序和定製排序

公共類:

	package com.ex;
	
	import java.util.Objects;
	
	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 +
	                '}';
	    }
	
	    @Override
	    public boolean equals(Object o) {
	        if (this == o) return true;
	        if (!(o instanceof MyDate)) return false;
	        MyDate myDate = (MyDate) o;
	        return getDay() == myDate.getDay() &&
	                getMonth() == myDate.getMonth() &&
	                getYear() == myDate.getYear();
	    }
	
	    @Override
	    public int hashCode() {
	        return Objects.hash(getDay(), getMonth(), getYear());
	    }
	}

自然排序,實現comparable介面,重寫compareTo方法,按照名字排序

package com.ex;

import java.util.Objects;

public class Employee  implements Comparable{
    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) {
        super();
        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 instanceof Employee)) return false;
        Employee employee = (Employee) o;
        return getAge() == employee.getAge() &&
                Objects.equals(getName(), employee.getName()) &&
                Objects.equals(getBirthday(), employee.getBirthday());
    }

    @Override
    public int hashCode() {

        return Objects.hash(getName(), getAge(), getBirthday());
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Employee){
            Employee e = (Employee) o;
            return this.name.compareTo(e.name);
        }
        return 0;
    }
}

實現類

	package com.ex;
	 
	import java.util.Iterator;
	import java.util.TreeSet;
	
	public class TestTree {
	public static void main (String[] args){
	    //自然排序
	    Employee e1 = new Employee("zzz", 55, new MyDate(4, 12, 1976));
	    Employee e2 = new Employee("ccc", 43, new MyDate(7, 3, 1965));
	    Employee e3 = new Employee("ooo", 33, new MyDate(9, 12, 1954));
	    Employee e4 = new Employee("ppp", 54, new MyDate(12, 2, 1967));
	    Employee e5 = new Employee("aaa", 65, new MyDate(4, 21, 1945));
	
	    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());
	    }
	}
	}

結果

Employee [name=aaa, age=65, birthday=MyDate{day=4, month=21, year=1945}]
Employee [name=ccc, age=43, birthday=MyDate{day=7, month=3, year=1965}]
Employee [name=ooo, age=33, birthday=MyDate{day=9, month=12, year=1954}]
Employee [name=ppp, age=54, birthday=MyDate{day=12, month=2, year=1967}]
Employee [name=zzz, age=55, birthday=MyDate{day=4, month=12, year=1976}]

定製排序:java-bean沒有實現comparable介面和重寫compareTo方法

package com.ex;

import java.util.Objects;

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) {
        super();
        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 instanceof Employee1)) return false;
        Employee1 employee = (Employee1) o;
        return getAge() == employee.getAge() &&
                Objects.equals(getName(), employee.getName()) &&
                Objects.equals(getBirthday(), employee.getBirthday());
    }

    @Override
    public int hashCode() {

        return Objects.hash(getName(), getAge(), getBirthday());
    }

}

實現類

//定製排序:建立 TreeSet 時傳入 Comparator物件,按生日日期的先後排序。

package com.ex;

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

public class TestTree1 {
    public static void main(String[] args) {
        Comparator com = new Comparator() {
            @Override
            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()) {
                        return birth1.getYear() - birth2.getYear();
                    } else {
                        if (birth1.getMonth() != birth2.getMonth()) {
                            return birth1.getMonth() - birth2.getMonth();
                        }else {
                            return birth1.getDay() - birth2.getDay();
                        }
                    }

                }
                return 0;
            }
        };
        
        Employee e1 = new Employee("zzz", 55, new MyDate(4, 12, 1976));
        Employee e2 = new Employee("ccc", 43, new MyDate(7, 3, 1965));
        Employee e3 = new Employee("ooo", 33, new MyDate(9, 12, 1954));
        Employee e4 = new Employee("ppp", 54, new MyDate(12, 2, 1967));
        Employee e5 = new Employee("aaa", 65, new MyDate(4, 21, 1945));

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

結果:

Employee [name=aaa, age=65, birthday=MyDate{day=4, month=21, year=1945}]
Employee [name=ccc, age=43, birthday=MyDate{day=7, month=3, year=1965}]
Employee [name=ooo, age=33, birthday=MyDate{day=9, month=12, year=1954}]
Employee [name=ppp, age=54, birthday=MyDate{day=12, month=2, year=1967}]
Employee [name=zzz, age=55, birthday=MyDate{day=4, month=12, year=1976}]