问题描述
这是一个例子:
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)));