我有以下格式的一系列輸入字符串:的Java 8 groupingby自定義鍵
typeA:code1,
typeA:code2,
typeA:code3,
typeB:code4,
typeB:code5,
typeB:code6,
typeC:code7,
...
,我需要得到Map<String, List<String>>
結構如下:
typeA, [code1, code2, code3]
typeB, [code4, code5, code6]
typeC, [code7, code8, ...]
美中不足的是爲了生成每種類型,我需要在每個輸入字符串上調用像這樣的函數:
public static String getType(String code)
{
return code.split(":")[0]; // yes this is horrible code, it's just for the example, honestly
}
I'我非常相信Streams和Collectors可以做到這一點,但我正在努力使正確的咒語成爲可能。
謝謝,這是非常接近我想出來的。我意識到groupingBy中第一個參數的類型是一個func,我可以在那裏使用我的getType函數。 – endian