Skip to content

Instantly share code, notes, and snippets.

@beall49
Created April 23, 2019 14:56
Show Gist options
  • Save beall49/d96b4dfa6cc2c8e1782d808caf6ec55c to your computer and use it in GitHub Desktop.
Save beall49/d96b4dfa6cc2c8e1782d808caf6ec55c to your computer and use it in GitHub Desktop.
private static Map<String, List<SomeObject>> groupListToMapById(List<SomeObject> items) {
return items.stream()
.collect(groupingBy(SomeObject::getId, toList()))
.entrySet()
.stream()
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(a, b) -> {throw new AssertionError();},
LinkedHashMap::new
));
}
private static List<SomeObject> getListFromMap(Map<String, List<SomeObject>> items) {
return items.values().stream()
.filter(e -> !e.isEmpty())
.flatMap(Collection::stream)
.collect(toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment