user3342330
user3342330

Reputation:

How to reverse iterate a nested multimap

I have a nested mulimap and need to iterate the outer level in reverse order.

private Multimap<Integer, Multimap<String, String>> stockMatrix 
    = ArrayListMultimap.create();

forward iteration is fine

for (Multimap<String, String> oneRow : stockMatrix.values()) {
..........
}

I looked at using a for loop from stockMatrix.size() but that gives me the count of all inner MultiMap pairs instead of just the number in the outer level.

Upvotes: 0

Views: 875

Answers (1)

fge
fge

Reputation: 121750

Slurp all of your values into a List and reverse that List, then iterate over it:

final List<Multimap<String, String>> allValues 
    = Lists.newArrayList(stockMatrix.values());

Collections.reverse(allValues);

// iterate over allValues

Upvotes: 1

Related Questions