stream汇总

自存做记录,很多不规范和简略的地方

(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中符合条件的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值