Java中的集合排序(自然排序 & 定制排序)

1. 定义一个Employee类,

该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;

并为每一个属性定义 getter, setter 方法;

并重写 toString 方法输出 name, age, birthday

 

MyDate类包含:

private成员变量month,day,year;并为每一个属性定义 getter, setter 方法;

 

创建该类的 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)

分别按以下两种方式对集合中的元素进行排序,并遍历输出:

 

1). 使Employee 实现 Comparable 接口,并按 name 排序

2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。


package A;

/**
 * Created by left on 17/10/13.
 */
public class MyDate {
    private int year;
    private int month;
    private int day;

    public int getYear() {
        return year;
    }

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

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

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

    public String toString() {
        String str = year + "年" + month + "月" + day + "日";
        return str;
    }

}

package A;

/**
 * Created by left on 17/10/13.
 */
@SuppressWarnings("rawtypes")
public class Employee implements Comparable {
    private int age;
    private String name;
    private MyDate birthday;

    public Employee(int age, String name, MyDate birthday) {
        this.age = age;
        this.name = name;
        this.birthday = birthday;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public String toString() {
        String str = "年龄:" + age + "  姓名:" + name.toString() + "  生日:" + birthday.toString();
        return str;
    }

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

}

package A;

/**
 * Created by left on 17/10/13.
 */

import org.junit.Test;

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

public class Test_Lei {
    /*
    * 自然排序
    */
    @Test
    public void test1() {

        TreeSet<Employee> ts = new TreeSet<Employee>();

        ts.add(new Employee(20, "AA", new MyDate(1994, 01, 3)));
        ts.add(new Employee(19, "FF", new MyDate(1995, 06, 8)));
        ts.add(new Employee(19, "DD", new MyDate(1995, 05, 14)));
        ts.add(new Employee(21, "CC", new MyDate(1993, 12, 20)));
        ts.add(new Employee(22, "BB", new MyDate(1992, 06, 12)));
        ts.add(new Employee(31, "EE", new MyDate(1983, 01, 12)));

        // System.out.println(ts);

        Object[] e;
        e = ts.toArray();
        for (int i = 0; i < ts.size(); i++) {
            System.out.println(e[i]);
        }
    }

    /*
    * 定制排序
    */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Test
    public void test2() {

        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o2 instanceof Employee) {
                    Employee c1 = (Employee) o1;
                    Employee c2 = (Employee) o2;
                    if (c1.getBirthday().getYear() == c2.getBirthday().getYear()) {
                        if (c1.getBirthday().getMonth() == c2.getBirthday().getMonth()) {
                            if (c1.getBirthday().getDay() == c2.getBirthday().getDay())
                                return 0;
                            else if (c1.getBirthday().getDay() > c2.getBirthday().getDay())
                                return -1;
                            else
                                return 1;
                        } else if (c1.getBirthday().getMonth() > c2.getBirthday().getMonth())
                            return -1;
                        else
                            return 1;
                    } else if (c1.getBirthday().getYear() > c2.getBirthday().getYear())
                        return -1;
                    else
                        return 1;
                }
                return 0;
            }
        };

        TreeSet ts = new TreeSet(comparator);

        ts.add(new Employee(20, "AA", new MyDate(1994, 01, 3)));
        ts.add(new Employee(19, "FF", new MyDate(1995, 06, 8)));
        ts.add(new Employee(19, "DD", new MyDate(1995, 05, 14)));
        ts.add(new Employee(21, "CC", new MyDate(1993, 12, 20)));
        ts.add(new Employee(22, "BB", new MyDate(1992, 06, 12)));
        ts.add(new Employee(31, "EE", new MyDate(1983, 01, 12)));

        Object[] e;
        e = ts.toArray();
        for (int i = 0; i < ts.size(); i++) {
            System.out.println(e[i]);
        }
    }

}


  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值