functional programming - javaslang List of Tuples2 to Map transormation -


what idiomatic way transform stream<tuple2<t,u>> map<t,list<u>> javaslang 2.1.0-alpha?

    // initial stream     stream.of(             tuple.of("foo", "x"),             tuple.of("foo", "y"),             tuple.of("bar", "x"),             tuple.of("bar", "y"),             tuple.of("bar", "z")     )         

should become:

    // end result     hashmap.ofentries(             tuple.of("foo", list.of("x","y")),             tuple.of("bar", list.of("x","y","z"))     ); 

not sure if idiomatic job foldleft:

stream    .of(       tuple.of("foo", "x"),       tuple.of("foo", "y"),       tuple.of("bar", "x"),       tuple.of("bar", "y"),       tuple.of("bar", "z")    )    .foldleft(       hashmap.empty(),        (map, tuple) ->           map.put(tuple._1, map.getorelse(tuple._1, list.empty()).append(tuple._2))    ); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -