use*_*115 3 java java-8 java-stream
如果我有类似的课程
public class Property {
private String id;
private String key;
private String value;
public Property(String id, String key, String value) {
this.id = id;
this.key = key;
this.value = value;
}
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我有Set<Property> properties一些属性,我想减少到Map这些Property对象的键和值.
我的大多数解决方案最终都不那么温文尔雅.我知道有一种方便的方法来做这些,Collector但我还不熟悉Java8.有小费吗?
Set<Property> properties = new HashSet<>();
properties.add(new Property("0", "a", "A"));
properties.add(new Property("1", "b", "B"));
Map<String, String> result = properties.stream()
.collect(Collectors.toMap(p -> p.key, p -> p.value));
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
556 次 |
| 最近记录: |