Map<String, String> phoneBook=people.stream()
.collect(toMap(Person::getName, Person::getAddress));
Run Code Online (Sandbox Code Playgroud)
重复发生时,我得到重复的密钥异常.
是否可以忽略在重复发生时将值添加到映射?
当存在重复时,它应该继续忽略该重复键.
我知道如何List从Y- > "转换"一个简单的Java Z,即:
List<String> x;
List<Integer> y = x.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
现在我想用Map做基本相同的事情,即:
INPUT:
{
"key1" -> "41", // "41" and "42"
"key2" -> "42 // are Strings
}
OUTPUT:
{
"key1" -> 41, // 41 and 42
"key2" -> 42 // are Integers
}
Run Code Online (Sandbox Code Playgroud)
解决方案不应限于String- > Integer.就像List上面的例子一样,我想调用任何方法(或构造函数).
我想创建一个Map从List的PointS和有地图从里面用相同的parentId,如映射列表中的所有条目Map<Long, List<Point>>.
我用过Collectors.toMap()但不编译:
Map<Long, List<Point>> pointByParentId = chargePoints.stream()
.collect(Collectors.toMap(Point::getParentId, c -> c));
Run Code Online (Sandbox Code Playgroud)