Map values to ArrayList

Viewed 25447

I have a Map<Integer, MyClass> and MyClass has 2 fields, Object1 obj and Object2 objj for example.

How can I create an ArrayList<Object2> with all Object2 values?

Must I iterate the Map and then add the values to the ArrayList or exists another way?

3 Answers

Checkout following :

How to convert a Map to List in Java?

It has one liner snippet for your question.

List<Object2> list = new ArrayList<Object2>(map.values());

assuming:

Map<Integer, MyClass> map;
Related