1.遍历 foreach
entityList.stream().forEach(System.out::println);2.map,entityList 转为 map<Long,String> id -> name
Map<Long, String> groupNameMap = groupEntityList.stream().collect(Collectors.toMap(QuestionGroupEntity::getId, QuestionGroupEntity::getGroupName));3.groupingBy-分组,entityList 转为 map<Long,List> typeId -> List
Map<Long, List<QuestionDTO>> map = dtoList.stream().collect(Collectors.groupingBy(QuestionDTO::getTypeId));4.filter-筛选指定条件的集合
dtoList.stream().filter(d -> currentIdSet.contains(d.getId())).collect(Collectors.toList())5.limit
entityList.stream().limit(2).collect(Collectors.toList());6.sorted 排序
Comparator.comparing(AnswerDTO::getShowNo)是使用排序器指定具体按什么字段排序(正序-从小到大)
answerDTOList = answerDTOList.stream().sorted(Comparator.comparing(AnswerDTO::getShowNo)).collect(Collectors.toList());reversed()是倒序
answerDTOList = answerDTOList.stream().sorted(Comparator.comparing(AnswerDTO::getShowNo).reversed()).collect(Collectors.toList());7.返回值的问题 最后是个使用提醒,使用stream对流进行操作后除非是直接return,否则必须
赋给原集合 list = list.stream()... 使用新集合 newList = list.stream()... 错误的写法 list.stream()...; // 类似作用域问题,list还是原来的没变化 return list;