Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Last active February 18, 2019 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomwhoiscontrary/d5aacbd1ceeb472e83c87bb374306fd8 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/d5aacbd1ceeb472e83c87bb374306fd8 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Combine {
public static class Item {
String name;
long value;
public String getName() {
return name;
}
public long getValue() {
return value;
}
}
public List<Item> combine(List<Item> defaults, List<Item> overrides) {
Map<String, Item> overridesByName = overrides.stream()
.collect(Collectors.toMap(Item::getName,
i -> i,
(older, newer) -> newer,
LinkedHashMap::new));
return defaults.stream()
.map(i -> overridesByName.getOrDefault(i.getName(), i))
.collect(Collectors.toCollection(ArrayList::new));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment