Stream流的Lambda表达式简介
各类用法
列表操作
Sort用法
- 排序(sort+comparator)
objectXList.sort(Comparator.comparing(ObjectX::getSort));
map用法
- 实体映射到字段(map+Collectors)
List<FieldA.class> resultList = objectXList.stream.map(Obj -> Obj.getFieldA()).collect(Collectors.toList());
match/noneMatch用法
- anyMatch任意匹配成功则视为true
boolean b = objectXList.stream.anyMatch(customList::contains);
- allMatch全匹配成功才视为true
boolean b = objectXList.stream.allMatch(customList::contains);
- noneMatch没有任何匹配成功才视为true
boolean b = objectXList.stream.noneMatch(customList::contains);
filter用法
-
全列表筛选(filter+Collectors)
List<ObjectX> resultList = objectXList.stream.filter(e->e.getSublist().isNotEmpty()).collect(Collectors.toList());