jdk1.8 List中的stream的groupby操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012190514/article/details/86174234

这里简单说一下jdk1.8中新的stream操作中的一个,grouppingby。

这个方法非常的实用,它可以非常迅速的将实体类中的中数据进行分组获取。

举例:

//a
Map<Long,List<Long>> exhibitionPitemMap = list.stream().collect(Collectors.groupingBy(TestDTO1::getLevle1CategoryId, Collectors.mapping(TestDTO1::getPitemId, Collectors.toList())));
//b
Map<Long, List<TestDTO2>> categoryPitemMap = list.stream().collect(Collectors.groupingBy(TestDTO2::getLevle1CategoryId));

在上述的a代码中,我们从一个实体类为TestDTO1的list中获取level1CategoryId这个属性与它对应的pitemId的List的Map。

在上述b代码中,我们根据level1CategoryId将原有的list进行分组成这个level1CategoryId以及他的List<TestDTO2>集合Map。

猜你喜欢

转载自blog.csdn.net/u012190514/article/details/86174234
今日推荐