Shiny_and_Chrome
Shiny_and_Chrome

Reputation: 320

Java: Access element of an arrayList which is a value in a TreeMap

I have a TreeMap which maps a key to an ArrayList. I would like access each of the elements stored inside each ArrayList of the TreeMap.

Is there a method to do this?

The elements of the ArrayList are of type String.

Upvotes: 0

Views: 98

Answers (1)

Guillaume F.
Guillaume F.

Reputation: 6463

I understand that you want to loop over all the elements in your TreeMap lists. You can achieve this easily with Java 8 streams:

treeMap.values().stream().flatMap(List::stream).forEach(element -> doStuff(element));

For more information, please refer to:
http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html

Upvotes: 1

Related Questions