Collect from stream into a multimap

You can just use this:

List<String> words = Arrays.asList("a b c d e a b c".split("\s+"));
Multimap<String, Integer> tokenMap = IntStream.range(0, words.size()).boxed()
        .collect(ArrayListMultimap::create, (m, i) -> m.put(words.get(i), i), Multimap::putAll);

The result will be:

{a=[0, 5], b=[1, 6], c=[2, 7], d=[3], e=[4]}
原文地址:https://www.cnblogs.com/zzt-lovelinlin/p/13452801.html