java8使用Lambda表达式list转map-含实例解释

Public Map<String, String> findAllMap() {
    List<CourseType> courseTypeList = findAll();
    if (ObjectUtils.isNotNull(courseTypeList))
        return 
courseTypeList.stream().collect(Collectors.toMap(CourseType:: getTypeKey, CourseType::getTypeName));
}

解释:

目标:得到一个Map<string,string>;

把courseTypeList流化(只有获取stream后才能进行后边的操作),

调用collect()重新统计成一个map<string,string>,

key,value分别使用CourseType类中的getTypeKey()和getTypeName()方法

等同于:

Map<String, String> map=new HashMap<String, String>();
for(CourseType c:courseTypeList){
    map.put(c.getTypeKey(),c.getTypeName());
}

-----------------------------------------------------------------------------------------------------------------

Map<Long, Long> subjectIdMap = Stream.of(condition.split(","))
                .filter(StringUtils::isNumber)
                .map(Long::valueOf)
                .collect(Collectors.toMap(Function.identity(), Function.identity()));

        List<Long> courseIdList = new LinkedList<>();
        courses.forEach((v) -> {
            if (!subjectIdMap.containsKey(v.getSubjectId())) {
                courseIdList.add(v.getId());
            }
        });

解释:

        目标:得到一个Map<Long, Long>

        split将condition按逗号分割
        Stream.of转换对应的流
        filter获取其中的数字
        map转换成long类型
        collect合并成一个map,key和value都是转换后的数字!

----------------------------------------------------------------------------------------------------------------

Map<BigDecimal,SysTeacher> teacherMap = teacherList.stream().collect(Collectors.toMap(e -> e.getId(),e -> e));

解释:

        目标:得到一个Map<BigDecimal,SysTeacher>

把teacherList流化(只有获取stream后才能进行后边的操作),

调用collect()重新统计成一个Map<BigDecimal,SysTeacher>,

key,value分别使用SysTeacher类中的getId()和SysTeacher实体。

-------------------------------------------------------------------------------------------------------------------

List<Shop> shopList=shopBiz.getShopList(shopData,userId);

Map<String,List<Shop>> shopListMap = shopList.stream().collect(Collectors.groupingBy(e->e.getShopType()));

解释:

        目标:得到一个Map<String,List<Shop>>

以getShopType分组,key是getShopType,value是List<Shop>>

----------------------------------------------------------------------------------------------------------------------------------

packageCourseMap.forEach((k, v) -> {

Map<Boolean, List<PackageCourse>> packageCourseListMap = v.stream().collect(Collectors.groupingBy(e -> StringUtils.isNotEmpty(e.getCourseTypeKey())));

}

解释:

同上,只是加了过滤条件而已

----------------------------------------------------------------------------------------------------------------------------------

返回类型:Map<BigDecimal,SysTeacher>

List<SysTeacher> sysTeacherList;

return sysTeacherList.stream().collect(Collectors.toMap(BaseEntity::getId, e->e));

解释:

       将List<SysTeacher>转为Map<BigDecimal,SysTeacher>,SysTeacher.getId()作为key.                                                                    

----------------------------------------------------------------------------------------------------------------------------------

Map<BigDecimal, String> group = orderDetails.stream() .collect(Collectors.groupingBy(OrderDetails::getShopId, Collectors.mapping((v) -> v.getUserId().toString(),
        Collectors.collectingAndThen(Collectors.toList(), (v) -> v.stream().distinct().collect(joining(","))))));

解释:

key就是ShopId,String就是去重复后的用户id,按逗号拼接起来

----------------------------------------------------------------------------------------------------------------------------------

Map<String, Integer> statisticsMap = Stream.of(classesIds.split(",")).collect(Collectors.toMap(Function.identity(), (v) -> 1));

解释:

Stream.of(classesIds.split(",")) 是将ids按逗号切割,然后返回一个流。

collect是收集。

Collectors.toMap(Function.identity(), (v) -> 1)),是将上面的流,转换成map。

Function.identity()是转成map的key,1是转成map的value。

它的意思是,返回一个总是返回其输入参数的函数。

其实就是上面的逗号截取后的每个值。

如果classesIds是 11,22,33
返回的map是:
11,1
22,1
33,1

发布了114 篇原创文章 · 获赞 52 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Smile_Sunny521/article/details/89845647