使用输入作为键映射器将String集合转换为Map

问题描述

这是一个例子:

Map<String,Student> getStudentsById(Collection<String> ids) {

  return ids.stream()
       .collect(Collectors.toMap(<id-here>,id -> new Student(id))

}

我不确定如何使用Collectors.toMap,因此key是流元素(这里是ID),而value是根据{{ 1}}。

解决方法

您应该将 '<?xml version="1.0" encoding="utf-8"?> <Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Filename>1234</Filename> <Sequence Type="FRONT"> <Object> <Value>3421</Value> <Value>John</Value> </Object> <Object> <Value>1234</Value> <Value>SAM</Value> </Object> </Sequence> </Data>' String传递给Student时将Collectors.toMap()Function<? super String,? extends String>传递给Function<? super String,? extends Student>

应该是:

Map<String,Student> idToStudent = ids.stream()
     .collect(Collectors.toMap(Function.identity(),id -> new Student(id)));

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...