Stream流的谓词逻辑和并行流计算

public class Employer {

    private Integer id;
    private Integer age;   //年龄
    private String gender;  //性别
    private String firstName; //名字
    private String lastName; //姓氏
    public static Predicate<Employer> ageGreaterThan70 = x -> x.getAge() >70;
    public static Predicate<Employer> genderM = x -> x.getGender().equals("M");

    public Integer getId() {
        return id;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Employer(Integer id, Integer age, String gender, String firstName, String lastName) {
        this.id = id;
        this.age = age;
        this.gender = gender;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Employer() {
    }

    @Override
    public String toString() {
        return "Employer{" +
                "id=" + id +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}
public static void main(String[] args) {
        Employer e1 = new Employer(1,23,"M","Rick","Beethovan");
        Employer e2 = new Employer(2,13,"F","Martina","Hengis");
        Employer e3 = new Employer(3,43,"M","Ricky","Martin");
        Employer e4 = new Employer(4,26,"M","Jon","Lowman");
        Employer e5 = new Employer(5,19,"F","Cristine","Maria");
        Employer e6 = new Employer(6,15,"M","David","Feezor");
        Employer e7 = new Employer(7,68,"F","Melissa","Roy");
        Employer e8 = new Employer(8,79,"M","Alex","Gussin");
        Employer e9 = new Employer(9,15,"F","Neetu","Singh");
        Employer e10 = new Employer(10,45,"M","Naveen","Jain");
        List<Employer> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
//        List<Employer> list = employees.stream().filter(e -> e.getAge() > 70 && e.getGender().equals("M")).collect(Collectors.toList());
//        System.out.println(list);

        //使用可复用谓词逻辑
        List<Employer> filterAnd = employees.stream().filter(Employer.ageGreaterThan70.and(Employer.genderM)).collect(Collectors.toList());
        System.out.println(filterAnd);
        List<Employer> filterOr = employees.stream().filter(Employer.ageGreaterThan70.or(Employer.genderM)).collect(Collectors.toList());
        System.out.println(filterOr);
        List<Employer> filterNegate = employees.stream().filter(Employer.ageGreaterThan70.or(Employer.genderM).negate()).collect(Collectors.toList());
        System.out.println(filterNegate);

        System.out.println("------------------");
        //每人涨一岁,性别换全词
        List<Employer> map = employees.stream().map(e -> {
            e.setAge(e.getAge() + 1);
            e.setGender(e.getGender().equals("M") ? "F" : "M");
            return e;
        }).collect(Collectors.toList());
        System.out.println(map);
        
    }
  //先用map将Stream流中的元素由Employee类型处理为Integer类型(age)。
        //然后对Stream流中的Integer类型进行归约
        Integer total = employees.stream().map(Employer::getAge).reduce(0, Integer::sum);
        System.out.println(total);

        //除了使用map函数实现类型转换后的集合归约,我们还可以用Combiner合并器来实现,这里第一次使用到了Combiner合并器。
        //        * 因为Stream流中的元素是Employee,累加器的返回值是Integer,所以二者的类型不匹配。
        //        * 这种情况下可以使用Combiner合并器对累加器的结果进行二次归约,相当于做了类型转换

        Integer total2 = employees.stream().reduce(0, (totalAge, emp) -> totalAge + emp.getAge(), Integer::sum);
        System.out.println(total2);

        //并行流数据归约(使用合并器)
        // 在进行并行流计算的时候,可能会将集合元素分成多个组计算。为了更快的将分组计算结果累加,可以使用合并器。
        Integer total3 = employees.parallelStream().map(Employer::getAge).reduce(0, Integer::sum,Integer::sum);
        System.out.println(total3);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值