Java之List快速过滤和修改属性值
参考链接:
https://blog.csdn.net/xinyang12138/article/details/86526540
https://blog.csdn.net/liuming690452074/article/details/84062011
Lambda 表达式的语法格式:
1.(parameters) -> expression
2.(parameters) ->{ statements; }
Lambda 表达式的简单例子:
1.不需要参数,返回值为 5
() -> 5
2. 接收一个参数(数字类型),返回其2倍的值
x -> 2 * x
3. 接受2个参数(数字),并返回他们的差值
(x, y) -> x – y
4. 接收2个int型整数,返回他们的和
(int x, int y) -> x + y
5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)
(String s) -> System.out.print(s)
list.stream().filter()根据条件过滤List
1.输出符合表达式的每一个对象
employees.stream().filter(p -> p.getAge() > 21).forEach(System.out::println);
2.返回一个符合表达式的集合
employees.stream().filter(predicate).collect(Collectors.<Employee>toList());
3.返回符合表达式的集合的第一个对象
employees.stream().filter(p -> p.getAge() > 21).findFirst();
4.抽取对象中所有的id的集合
employees.stream().map(Employee::getId).collect(Collectors.toList());
list.forEach()设置单个或多个属性值
1.设置单个属性值
list.forEach((entity) -> entity.setImprestId(aaEntity.getId()));
1.设置多个属性值
list.forEach((entity) ->{
entity.setCreateTime(new Date());
entity.setCreateUser(ShiroUtils.getUserId());
entity.setContractId(hTity.getId());
});