以下为个人在工作中的stream转换操作的使用经验,如有失误,请指正!
一、List转Map
1、List的元素为对象,Map的key为对象的某个属性,Map的value为整个对象
使用lambda表达式
Map<String,BaseUserProperty> userIdMap=Optional.ofNullable(BaseUserList).orElse(new ArrayList<BaseUserProperty>()) .stream().collect(Collectors.toMap(BaseUserProperty::getUserId,Function.identity()));
使用箭头函数
Map<String,BaseUserProperty> userIdMap=Optional.ofNullable(BaseUserList).orElse(new ArrayList<BaseUserProperty>()) .stream().collect(Collectors.toMap(item->item.getUserId(),item->item));
如果key有重复时的处理方式
Map<String,BaseUserProperty> userIdMap=Optional.ofNullable(BaseUserList).orElse(new ArrayList<BaseUserProperty>()) .stream().collect(Collectors.toMap(item->item.getUserId(),item->item,(key1,key2)->key1));
指定生成LinkedHashMap
Map<String,BaseUserProperty> userIdMap=Optional.ofNullable(BaseUserList).orElse(new ArrayList<BaseUserProperty>()) .stream().collect(Collectors.toMap(BaseUserProperty::getUserId,Function.identity(),(key1,key2)->key1,LinkedHashMap::new));
2、List的元素为Map,需要转换的Map的key为元素Map的某个key,需要转换的Map的value为整个元素Map
使用箭头函数
List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>(); Map<String,Map<String,Object>> objMap=Optional.ofNullable(mapList).orElse(new ArrayList<Map<String,Object>>()) .stream().collect(Collectors.toMap(map->String.valueOf(map.get("key")),item->item));
重复key处理
List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>(); Map<String,Map<String,Object>> objMap=Optional.ofNullable(mapList).orElse(new ArrayList<Map<String,Object>>()) .stream().collect(Collectors.toMap(map->String.valueOf(map.get("key")),item->item,(key1,key2)->key1));
指定生成LinkedHashMap
Map<String,Object> classTypeKeyColumnClassMap= Optional.ofNullable(classTypeKeyList).orElse(new ArrayList<String>()) .stream().collect(Collectors.toMap(item->item,item->"0",(key1,key2)->key1,LinkedHashMap::new));
二、List<T>转List<T2>
1、T为对象,T2为对象的某个属性
使用lambda表达式
List<String> userIds=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList<ScheduleInfoDTO>()) .stream().map(ScheduleInfoDTO::getUserId).collect(Collectors.toList());
使用箭头函数
List<String> userIds=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList<ScheduleInfoDTO>()) .stream().map(item -> item.getUserId()).collect(Collectors.toList());
2、T为Map,T2为Map的某个key
List<String> vacationKeyList=Optional.ofNullable(vacationMapList).orElse(new ArrayList<Map<String,Object>>()).stream() .map(tempMap -> String.valueOf(tempMap.get("key"))).collect(Collectors.toList());
三、List<T>转Map<k,List<T>>
1、T为对象,k为对象的某个属性
使用lambda表达式
Map<String,List<ScheduleInfoDTO>> userScheduleListMap=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList<ScheduleInfoDTO>()).stream() .collect(Collectors.groupingBy(ScheduleInfoDTO::getUserId));
使用箭头函数
Map<String,List<ScheduleInfoDTO>> userScheduleListMap=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList<ScheduleInfoDTO>()).stream() .collect(Collectors.groupingBy(item->item.getUserId()));
指定生成LinkedHashMap
Map<String,List<ScheduleInfoDTO>> userScheduleListMap=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList<ScheduleInfoDTO>()).stream() .collect(Collectors.groupingBy(ScheduleInfoDTO::getUserId,LinkedHashMap::new,Collectors.toList()));
2、T为Map,k为Map的key
使用箭头函数
List<Map<String,Object>> weekScheduleInfoDTOList1=new ArrayList<Map<String,Object>>(); Map<String,List<Map<String,Object>>> userScheduleListMap2=Optional.ofNullable(weekScheduleInfoDTOList1).orElse(new ArrayList<Map<String,Object>>()).stream() .collect(Collectors.groupingBy(item-> String.valueOf(item.get("key"))));
指定生成LinkedHashMap
List<Map<String,Object>> weekScheduleInfoDTOList1=new ArrayList<Map<String,Object>>(); Map<String,List<Map<String,Object>>> userScheduleListMap2=Optional.ofNullable(weekScheduleInfoDTOList1).orElse(new ArrayList<Map<String,Object>>()).stream() .collect(Collectors.groupingBy(item-> String.valueOf(item.get("key")),LinkedHashMap::new,Collectors.toList()))
发表评论