Dav*_*err 28 java java-8 java-stream
鉴于一个Map<String, Person>地方的人有一个String getName()就可以了(等)的方法,我怎么能转Map<String, Person>成Map<String, String>,其中String从调用获得Person::getName()?
我会使用Pre-Java 8
Map<String, String> byNameMap = new HashMap<>();
for (Map.Entry<String, Person> person : people.entrySet()) {
byNameMap.put(person.getKey(), person.getValue().getName());
}
Run Code Online (Sandbox Code Playgroud)
但是我想用流和lambdas来做.
我无法看到如何以功能样式执行此操作:Map/HashMap不实现Stream.
people.entrySet()返回Set<Entry<String, Person>>我可以流过的内容,但是如何Entry<String, String>向目标地图添加新内容?
Nic*_*olt 36
使用Java 8,您可以:
Map<String, String> byNameMap = new HashMap<>();
people.forEach((k, v) -> byNameMap.put(k, v.getName());
Run Code Online (Sandbox Code Playgroud)
虽然你最好使用Guava的Maps.transformValues,它包装了原始版本Map并在你执行转换时进行转换get,这意味着你只需在实际使用该值时支付转换成本.
使用Guava看起来像这样:
Map<String, String> byNameMap = Maps.transformValues(people, Person::getName);
Run Code Online (Sandbox Code Playgroud)
编辑:
继@ Eelco的评论(和完整性),转换到地图是更好下来Collectors.toMap是这样的:
Map<String, String> byNameMap = people.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().getName());
Run Code Online (Sandbox Code Playgroud)
ass*_*ias 21
一种方法是使用toMap收集器:
import static java.util.stream.Collectors.toMap;
Map<String, String> byNameMap = people.entrySet().stream()
.collect(toMap(Entry::getKey,
e -> e.getValue().getName()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9001 次 |
| 最近记录: |