一、过滤 filter
1.方式一
List<Organization> collect = organizations.stream().filter(c -> "wer".equals(c.getName())).collect(Collectors.toList());
2.方式二
List<Organization> collect1 = organizations.stream().filter(c -> {
if ("234".equals(c.getName()) && "123".equals(c.getCode())) {
return true;
}
return false;
}).collect(Collectors.toList());
二、过滤筛选列 map
List<String> collect2 = organizations.stream().map(Organization::getId).collect(Collectors.toList());
三、去重
List<Organization> collect3 = organizations.stream().distinct().collect(Collectors.toList());
四、for循环 forEach
1.方式一
organizations.stream().forEach(c -> c.setName("123"));
2.方式二
organizations.stream().forEach(c -> {
if ("123".equals(c.getName())) {
c.setName("456");
}
});
五、分页 limit
List<Organization> collect4 = organizations.stream().limit(10).collect(Collectors.toList());
六、长度 count
long count = organizations.stream().count();
int size = organizations.size();
七、Match
1. anyMatch
anyMatch 表示,判断的条件里,任意一个元素成功,返回true
boolean b = organizations.stream().anyMatch(c -> "123".equals(c.getName()));
2. allMatch
allMatch表示,判断条件里的元素,所有的都是,返回true
boolean b1 = organizations.stream().allMatch(c -> "123".equals(c.getName()));
3. noneMatch
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
boolean b2 = organizations.stream().noneMatch(c -> "123".equals(c.getName()));
|