Lambda和stream().filter()一起使用

2019-12-21  本文已影响0人  woods_deamon

Person p1 =new Person("张三", new BigDecimal("23.0"));

Person p2 =new Person("王五", new BigDecimal("64.0"));

Person p3 =new Person("李四", new BigDecimal("75.0"));

Person p4 =new Person("王五", new BigDecimal("50.0"));

Person p5 =new Person("赵六", new BigDecimal("15.0"));

List pList =new ArrayList<>();

pList.add(p1);

pList.add(p2);

pList.add(p4);

pList.add(p3);

pList.add(p5);

System.out.println("未做过滤:" + pList);

List collect = pList.stream().filter(item -> item.getSalary().compareTo(new BigDecimal("50")) >=0).collect(Collectors.toList());

System.out.println("做了过滤:" + collect);

打印结果:

未做过滤:[{name='张三', salary=23.0}, {name='王五', salary=64.0}, {name='王五', salary=50.0}, {name='李四', salary=75.0}, {name='赵六', salary=15.0}]

做了过滤:[{name='王五', salary=64.0}, {name='王五', salary=50.0}, {name='李四', salary=75.0}

上一篇下一篇

猜你喜欢

热点阅读