Skip to content

Instantly share code, notes, and snippets.

@Oregu
Last active August 29, 2015 14:01
Show Gist options
  • Save Oregu/7696491814560e7c94ea to your computer and use it in GitHub Desktop.
Save Oregu/7696491814560e7c94ea to your computer and use it in GitHub Desktop.
Reduce 2D collection in Java 8
Map<String, List<Field>> fieldmap = new HashMap<>();
fieldmap.put("f1", Arrays.asList(new Field("f_str"), new Field("f_abc")));
fieldmap.put("f2", Arrays.asList(new Field("f_int")));
// This is the best I came up with
List<String> names = fieldmap.values().stream().collect(
ArrayList<String>::new,
(l, fs) -> l.addAll(fs.stream().map(Field::getName).collect(toList())),
ArrayList<String>::addAll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment