码迷,mamicode.com
首页 > 编程语言 > 详细

java8 去重和排序

时间:2017-11-03 18:52:24      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:ceo   return   img   ble   public   reac   override   code   blog   

技术分享

 

技术分享
 /*
    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 去重和排序

标签:ceo   return   img   ble   public   reac   override   code   blog   

原文地址:http://www.cnblogs.com/caohuimingfa/p/7779012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!