给定Map<String, Person>
where Person 有一个String getName()
(等)方法,我怎样才能把它Map<String, Person>
变成一个Map<String, String>
where theString
是从调用中获得的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());
}
但我想使用流和 lambda 来做到这一点。
我看不到如何以功能样式执行此操作: Map/HashMap don't implement Stream
.
people.entrySet()
返回一个Set<Entry<String, Person>>
我可以流式传输的,但是如何Entry<String, String>
向目标地图添加一个新的?