Created
January 11, 2016 11:36
-
-
Save h0tk3y/dbb3754e93e044f4e841 to your computer and use it in GitHub Desktop.
Maps inversion
This file contains hidden or 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
| public <A, B, C> Map<B, List<Pair<A, C>>> invertMap(Map<A, Map<B, C>> input) { | |
| return input.entrySet().stream() | |
| .flatMap(kv -> kv.getValue().entrySet().stream().map(vs -> new Pair<>(kv.getKey(), vs))) | |
| .collect(Collectors.groupingBy( | |
| it -> it.getValue().getKey(), | |
| Collectors.mapping( | |
| it -> new Pair<>(it.getKey(), it.getValue().getValue()), | |
| Collectors.toList() | |
| )) | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment