Create a stream of the values in maps that are values in another map in Java

Viewed 134

Sorry about the title of the question; it was kind of hard for me to make sense of it. If you guys have a better title, let me know and I can change it.

I have two types of objects, Bookmark and Revision. I have one large Map, like so:

Map<Long, Bookmark> mapOfBookmarks;

it contains key: value pairs like so:

1L: Bookmark1,
2L: Bookmark2,
...

Each Bookmark has a 'getRevisions()' method that returns a Map

public Map<Long, Revision> getRevisions();

I want to create a Stream that contains all revisions that exist under mapOfBookmarks. Essentially I want to do this:

List<Revision> revisions = new ArrayList<>();
for (Bookmark bookmark : mapOfBookmarks.values()) { // loop through each bookmark in the map of bookmarks ( Map<Long, Bookmark> )
    for (Revision revision : bookmark.getRevisions().values()) { // loop through each revision in the map of revisions ( Map<Long, Revision> )
        revisions.add(revision);  // add each revision of each map to the revisions list
    }
}
return revisions.stream(); // return a stream of revisions

However, I'd like to do it using the functionality of Stream, so more like:

return mapOfBookmarks.values().stream().everythingElseThatIsNeeded();

Which would essentially be like saying:

return Stream.of(revision1, revision2, revision3, revision4, ...);

How would I write that out? Something to note is that the dataset that it is looping through can be huge, making the list method a poor approach.

I'm using Windows 7 and Java 8

2 Answers
Related