Skip to content

Instantly share code, notes, and snippets.

@diroussel
Created June 30, 2010 11:04
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 diroussel/458523 to your computer and use it in GitHub Desktop.
Save diroussel/458523 to your computer and use it in GitHub Desktop.
/**
* Returns an Iterable of all the values in all the submaps of the parent map.
*/
<K1, K2, T> Iterable<T> innerValues(Map<K1, Map<K2, T>> map) {
return Iterables.concat(
Iterables.transform(map.values(),
new Function<Map<K2, T>, Iterable<T>>() {
@Override
public Iterable<T> apply(Map<K2, T> innerMap) {
return innerMap.values();
}
}
)
);
}
@diroussel
Copy link
Author

This gist was written before java had streams. If I was doing this now, it would be like this: ...

    /**
     * Returns a Stream of all the values in all the submaps of the parent map.
     */
    <K1, K2, T> Stream<T> innerValues(Map<K1, Map<K2, T>> map) {
        return map.values().stream().flatMap(m2 -> m2.values().stream());
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment