Skip to content

Instantly share code, notes, and snippets.

@Randgalt
Last active December 7, 2015 01:45
Show Gist options
  • Save Randgalt/4cebb17ed8cd1724e1d0 to your computer and use it in GitHub Desktop.
Save Randgalt/4cebb17ed8cd1724e1d0 to your computer and use it in GitHub Desktop.
/*
Given a list of Things, return a map of key -> value where value is the
value of the highest version of the Thing with that key.
*/
public interface Thing {
String getKey();
String getValue();
int getVersion();
}
Collection<Thing> things = ...
Thing identityThing = ...
Map<String, String> latest = things.stream()
.collect(Collectors.groupingBy(Thing::getKey, Collectors.reducing(identityThing, (t1, t2) -> (t1.getVersion() > t2.getVersion()) ? t1 : t2)))
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getValue()))
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment