自存做记录,很多不规范和简略的地方
(1)forEach
map.entrySet().stream().forEach(e -> {
【这里可以调用跟e.getKey()、e.getValue(),比如a = a + e.getKey(),就可以给a加上map里面的key的值】
});
map.entrySet().stream().forEach(e -> {}
相当于for (Map.Entry<String, String> entry : map.entrySet()) {}
(2)groupingBy【List到Map】
①分组
xxBOList.stream().collect(Collectors.groupingBy(x -> x.getGrade()))
通过xxBO里的年级进行分组。
②排序
xxBOList.stream().collect(Collectors.groupingBy(UserQuestBO::getQuestId))
通过getQuestId排序
③累和
xxBOList.stream().collect(Collectors.groupingBy(UserQuestBO::getQuestId,Collectors.summingInt(UserQuestBO::getPaperId)))
④计数
xxBOList.stream().collect(Collectors.stream().collect(Collectors.groupingBy(UserQuestBO::getQuestId, Collectors.counting()))
(3)sorted
xxBOList.stream().sorted(Comparator.comparing(x->x.getGrade())).collect(Collectors.toList())
(4)map
xxBOList.stream().map(QuestBO::getId).collect(Collectors.toList())
List<xxBO>得到List<String>,依次从List<xxBO>里取想要的属性值,不用for循环更简便。
(5)toMap【List到Map】
xxBOList.stream().collect(Collectors.toMap(xxBOList::getId(), xxBOList::getName()))
将list转换为map,并设置key,value
·Collectors.toMap()中当key重复时,该方法默认会抛出IllegalStateException异常。
·当要转化的map的value值中包含空指针时, 会抛出NullPointerException异常。
(6)filter
xxBOList.stream().filter(x-> x.getId() == ”1”).collect(Collectors.toList())
x.getId() == ”1”是判断条件,会返回xxBOList中符合条件的值