我有以下格式的一系列输入字符串:的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