我有ElementCharacteristic的名单。我想循环遍历这个列表并构建一个Map。如何在中做到这一点?
public Map<String,String> getMap(List<ElementCharacteristic> inputList) {
Map<String,String> outPutMap = new HashMap<>();
for(ElementCharacteristic ele : inputList) {
outPutMap.put(ele.getCharacteristic(),ele.getFeature());
}
return outPutMap;
}发布于 2022-08-26 19:21:14
您可能可以使用以下代码:
Map<String, String> outPutMap = new HashMap<>();
inputList.stream()
.map(p->outPutMap.put(p.getCharacteristic(), p.getFeature()));或者这个:
Map<String,String> outPutMap = inputList .stream()
.collect(Collectors.toMap(p-> p.getCharacteristic(), p-> p.getFeature()));发布于 2022-08-27 18:10:09
您可以使用Collectors.toMap函数来实现这一点:
public Map<String,String> getMap(List<ElementCharacteristic> inputList) {
return inputList.stream()
.collect(Collectors.toMap(ElementCharacteristic::getCharacteristic, ElementCharacteristic::getFeature));
}https://stackoverflow.com/questions/73505510
复制相似问题