Collect Lines using Multimap Collector

前端 未结 1 1518
不思量自难忘°
不思量自难忘° 2021-01-12 09:14

Is there a way to covert the below to using collectors yet?

List lines = getLines();

Multimap multimap = ArrayListMulti         


        
相关标签:
1条回答
  • 2021-01-12 09:55

    You can use Multimaps.toMultimap collector:

    ListMultimap<String, String> multimap = lines.stream()
            .collect(Multimaps.toMultimap(
                    l -> l[0],
                    l -> l[1],
                    ArrayListMultimap::create
            ));
    

    Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector:

    ListMultimap<String, String> multimap = lines.stream()
            .collect(toImmutableListMultimap(l -> l[0], l -> l[1]));
    
    0 讨论(0)
提交回复
热议问题