java8 去重和排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8 去重和排序相关的知识,希望对你有一定的参考价值。

技术分享

 

技术分享
 /*
    Worker{id=1, name=‘曹操‘, gender=1, age=50, salary=50.56, workType=领导}
    Worker{id=2, name=‘典韦‘, gender=1, age=30, salary=30.56, workType=武将}
    Worker{id=3, name=‘荀彧‘, gender=1, age=40, salary=40.56, workType=谋士}
    Worker{id=4, name=‘刘备‘, gender=1, age=60, salary=60.56, workType=领导}
    */
    private static void  test4() {
        List<Worker> workerList = Arrays.asList(
                new Worker(1, "曹操", 1, 50, 50.56, WorkType.领导),
                new Worker(2, "典韦", 1, 30, 30.56, WorkType.武将),
                new Worker(3, "荀彧", 1, 40, 40.56, WorkType.谋士),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导),
                new Worker(4, "刘备", 1, 60, 60.56, WorkType.领导)
        );
        workerList.stream().distinct()
                .forEach(System.out::println);
    }
distinct()
技术分享
/**
 * Created by meicai on 2017/11/2.
 */
public class Worker{
    private int id;
    private String name;
    private int gender;
    private int age;
    private Double salary;
    private WorkType workType;

    public Worker(int id, String name, int gender, int age, Double salary, WorkType workType) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.salary = salary;
        this.workType = workType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public WorkType getWorkType() {
        return workType;
    }

    public void setWorkType(WorkType workType) {
        this.workType = workType;
    }

    @Override
    public String toString() {
        return "Worker{" +
                "id=" + id +
                ", name=‘" + name + ‘\\‘‘ +
                ", gender=" + gender +
                ", age=" + age +
                ", salary=" + salary +
                ", workType=" + workType +
                ‘}‘;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Worker)) return false;

        Worker worker = (Worker) o;

        if (getId() != worker.getId()) return false;
        if (getGender() != worker.getGender()) return false;
        if (getAge() != worker.getAge()) return false;
        if (!getName().equals(worker.getName())) return false;
        if (!getSalary().equals(worker.getSalary())) return false;
        return getWorkType() == worker.getWorkType();

    }

    @Override
    public int hashCode() {
        int result = getId();
        result = 31 * result + getName().hashCode();
        result = 31 * result + getGender();
        result = 31 * result + getAge();
        result = 31 * result + getSalary().hashCode();
        result = 31 * result + getWorkType().hashCode();
        return result;
    }
}

enum WorkType{
    领导,
    武将,
    谋士,
    士兵
}
Worker

 

以上是关于java8 去重和排序的主要内容,如果未能解决你的问题,请参考以下文章

大数据学习之十——MapReduce代码实例:数据去重和数据排序

ES6 set方法对数组去重和排序

Java——hashMap去重和排序

7数组去重和排序

去重和数组排序

根据list中对象的属性去重和排序小结