我知道如何“转换”一个简单的 Java List
从 Y
-> Z
,即:
List<String> x;
List<Integer> y = x.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
现在我想对地图做基本相同的事情,即:
INPUT:
{
"key1" -> "41", // "41" and "42"
"key2" -> "42" // are Strings
}
OUTPUT:
{
"key1" -> 41, // 41 and 42
"key2" -> 42 // are Integers
}
解决方案不应限于 String
-> Integer
。就像上面的 List
示例一样,我想调用任何方法(或构造函数)。
原文由 Benjamin M 发布,翻译遵循 CC BY-SA 4.0 许可协议
它不如列表代码好。您不能在
map()
调用中构造新的Map.Entry
s,因此工作混合到collect()
调用中。