Skip to content

Instantly share code, notes, and snippets.

@lukaseder
Last active August 29, 2016 14:22
Show Gist options
  • Save lukaseder/f291c47a22b84f83c15b8ed21cc16255 to your computer and use it in GitHub Desktop.
Save lukaseder/f291c47a22b84f83c15b8ed21cc16255 to your computer and use it in GitHub Desktop.
Better Map Key/Value collection with Java 8
@Test
public void testBlah() {
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
// Using Java 8 Stream
Tuple2<List<Integer>, List<String>> result =
map.entrySet()
.stream()
.collect(Tuple.collectors(
Collectors.mapping(Entry::getKey, Collectors.toList()),
Collectors.mapping(Entry::getValue, Collectors.toList())
));
// Using jOOL Seq
Tuple2<List<Integer>, List<String>> result =
Seq.seq(map)
.collect(
Collectors.mapping(Tuple2::v1, Collectors.toList()),
Collectors.mapping(Tuple2::v2, Collectors.toList())
);
System.out.println(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment