0

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.

1
  • You have a Map which maps a key to a List — you should ignore the fact that a TreeMap and an ArrayList were chosen as the implementation details, and program to the interfaces. This doesn't change the question and shouldn't affect the answer. Commented Mar 21, 2017 at 19:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.