用Comparator排序和分组

Test实体

import java.util.Objects;

/**
 * @author gallen
 * @description
 * @date 2018/11/16
 * @time 18:57
 */
public class Test {

    Integer id;

    String name;

    Integer age;

    Integer sex;

    public Test(Integer id, String name, Integer age, Integer sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return this.id;
    }

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

    public String getName() {
        return this.name;
    }

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

    public Integer getAge() {
        return this.age;
    }

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

    public Integer getSex() {
        return this.sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Test)) return false;
        Test test = (Test) o;
        return Objects.equals(this.getId(), test.getId()) &&
                Objects.equals(this.getName(), test.getName()) &&
                Objects.equals(this.getAge(), test.getAge()) &&
                Objects.equals(this.getSex(), test.getSex());
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getId(), this.getName(), this.getAge(), this.getSex());
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}

排序、分组

import java.util.Objects;

/**
 * @author gallen
 * @description
 * @date 2018/11/16
 * @time 18:57
 */
public class Demo {
    
    public static void main(String[] args) {
        List<Test> list = Lists.newArrayList();
        list.add(new Test(1, "zhangsan", 37, 1));
        list.add(new Test(2, "lisi", 35, 2));
        list.add(new Test(3, "wangwu", 25, 2));
        list.add(new Test(4, "zhaoliu", 67, 1));
        //排序
        Collections.sort(list, new Comparator<Test>() {
            @Override
            public int compare(Test o1, Test o2) {
                return o1.id.compareTo(o2.id);
            }
        });
        list1.stream().forEach(test -> {
            System.out.println("list1 = " + test.toString());
        });
        //分组
        List<List<Test>> list2 = divider(list, new Comparator<Test>() {
            @Override
            public int compare(Test o1, Test o2) {
                if (o1.age.intValue() > 30 && o2.age.intValue() > 30) {
                    return 0;
                } else if (o1.age.intValue() > 30 && o2.age.intValue() <= 30) {
                    return 1;
                } else if (o1.age.intValue() <= 30 && o2.age.intValue() > 30) {
                    return -1;
                } else if (o1.age.intValue() <= 30 && o2.age.intValue() <= 30) {
                    return 0;
                }else{
                    return 0;
                }
            }
        });
        list2.stream().forEach(test -> {
            System.out.println("list2 = " + test.toString());
        });
    }    
}

 

posted @ 2018-11-17 15:55  gallenlee978  阅读(225)  评论(0编辑  收藏  举报