谢邀
根据题主的描述,我感觉其实就是一个把map转化为map的过程,可以看到变化的只是有value而已,key没有变化,这里map也提供了一种变相来把map中数据转化为stream的方式,并没有直接map.stream()的方法
以前map循环是用到map的entrySet,而这里的entrySet就是一个集合,也就可以用stream了Map> map = new HashMap<>();map.put("java", Arrays.asList("1.7", "1.8"));map.entrySet().stream()
此时流map.entrySet().stream()里的数据是Map.Entry>,现在其实就是要把Map.Entry>转化为Map,由于最后是要的结果是用map收集,所以只能collect(Collectors.toMap())了Map collect = map.entrySet().stream() .collect(Collectors.toMap( stringListEntry -> stringListEntry.getKey(),
stringListEntry -> new Java(stringListEntry.getValue())));
Collectors.toMap的两个参数的方法,第一个参数代表如何获取key,第二个代表如何获取value,因为key没有变,所以直接取entry的key,value的话要转化为Java对象,所以写了一个构造函数(简单的构造方法我就用lombok注解了)@Getter
@Setter
@AllArgsConstructor
public static class Java{
private List versions;
}
差不多就是这样吧,仅供参考^_^