Last active
December 7, 2015 01:45
-
-
Save Randgalt/4cebb17ed8cd1724e1d0 to your computer and use it in GitHub Desktop.
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
/* | |
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