Reducing objects to maps using java 8 streams
•
Java
If I had a similar course
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
}
I have a set < property > I want to simplify the properties of some properties into a map with only the keys and values in these property objects
Most of my solutions end up being less elegant I know there is a convenient way to do this with collectors, but I'm not familiar with Java 8 Do you have a tip?
Solution
Set<Property> properties = new HashSet<>();
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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
