Last active
August 29, 2016 14:22
-
-
Save lukaseder/f291c47a22b84f83c15b8ed21cc16255 to your computer and use it in GitHub Desktop.
Better Map Key/Value collection with Java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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