java中排序使用方法

来源:互联网 发布:打的费用计算软件 编辑:程序博客网 时间:2024/05/20 07:49
package model;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.builder.ToStringBuilder;public class Person {    private String name;    private int    age;    private double salary;        private Date  birthday;    public Person() {    }        /**     * @param name     * @param age     * @param salary     * @param birthday     */    public Person(String name, int age, double salary, Date birthday) {        this.name = name;        this.age = age;        this.salary = salary;        this.birthday = birthday;    }    /**     * @param name     * @param age     * @param salary     */    public Person(String name, int age, double salary) {        this.name = name;        this.age = age;        this.salary = salary;    }    /**     * @param name     * @param age     */    public Person(String name, int age) {        this.name = name;        this.age = age;    }    /**     * Getter method for property <tt>name</tt>.     *      * @return property value of name     */    public String getName() {        return name;    }    /**     * Setter method for property <tt>name</tt>.     *      * @param name value to be assigned to property name     */    public void setName(String name) {        this.name = name;    }    /**     * Getter method for property <tt>age</tt>.     *      * @return property value of age     */    public int getAge() {        return age;    }    /**     * Setter method for property <tt>age</tt>.     *      * @param age value to be assigned to property age     */    public void setAge(int age) {        this.age = age;    }    /**     * Getter method for property <tt>salary</tt>.     *      * @return property value of salary     */    public double getSalary() {        return salary;    }    /**     * Setter method for property <tt>salary</tt>.     *      * @param salary value to be assigned to property salary     */    public void setSalary(double salary) {        this.salary = salary;    }            /**     * Getter method for property <tt>birthday</tt>.     *      * @return property value of birthday     */    public Date getBirthday() {        return birthday;    }    /**     * Setter method for property <tt>birthday</tt>.     *      * @param birthday value to be assigned to property birthday     */    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    /**      * @see java.lang.Object#toString()     */    @Override    public String toString() {        SimpleDateFormat format=new SimpleDateFormat("YYYY-MM-dd");        String finalBirthday="";        if(this.getBirthday()!=null){            finalBirthday=format.format(birthday);        }        return "Person [name=" + name + ", age=" + age + ", salary=" + salary + ", birthday="               + finalBirthday + "]";    }  }
package sort;import java.util.Comparator;import java.util.Date;import java.util.List;import java.util.stream.Collectors;import com.google.common.collect.Lists;import model.Person;public class PersonSort {    private void initPerson(List<Person> persons) {        Person p = new Person("whangsan", 12, 12.5, new Date(2017, 11, 12));        Person p1 = new Person("zhangsan", 13, 12.5, new Date(2017, 11, 13));        Person p2 = new Person("zhangsan", 13, 13.5, new Date(2017, 10, 12));        Person p3 = new Person("zhangsan", 13, 13.5, new Date(2017, 10, 9));        Person p4 = new Person("zhangsan", 11, 10.5, new Date(2017, 12, 11));        Person p5 = new Person("zhangsan", 11, 10.5);        persons.add(p);        persons.add(p1);        persons.add(p2);        persons.add(p3);        persons.add(p4);        persons.add(p5);    }    private void print(List<Person> persons) {        for (Person p : persons) {            System.out.println(p);        }    }    public static Comparator<Person> sort() {        Comparator<Person> sortByName = ((o1, o2) -> o1.getName().compareTo(o2.getName()));        Comparator<Person> sortByAge = ((o1, o2) -> Integer.compare(o1.getAge(), o2.getAge()));        Comparator<Person> sortBySalary = ((o1, o2) -> Double.compare(o1.getSalary(),            o2.getSalary()));        Comparator<Person> sortByDate = ((o1, o2) -> o1.getBirthday().compareTo(o2.getBirthday()));        Comparator<Person> sortByBirthday = ((o1, o2) -> Comparator.nullsFirst(            Comparator.<Date> reverseOrder()).compare(o1.getBirthday(), o2.getBirthday()));        return sortByName.thenComparing(sortByAge.reversed()).thenComparing(sortBySalary)            .thenComparing(sortByBirthday);    }    /**     *      * @param args     */    public static void main(String[] args) {        PersonSort personSort = new PersonSort();        List<Person> data = Lists.newArrayList();        List<Person> newdata = Lists.newArrayList();        personSort.initPerson(data);        personSort.print(data);        System.out.println("------");        newdata = data.stream().sorted(sort()).collect(Collectors.toList());        personSort.print(newdata);        System.out.println("------");        personSort.print(data);    }}