Java - Simple way to put LinkedHashMap keys/values into respective Lists?

user375566 picture user375566 · Dec 1, 2010 · Viewed 34.5k times · Source

I have a LinkedHashMap < String, String > map .

List < String > keyList;
List < String > valueList;

map.keySet();
map.values();

Is there an easy way to populate keyList from map.keySet() and valueList from map.values(), or do I have to iterate?

Answer

Bozho picture Bozho · Dec 1, 2010

Most collections accept Collection as a constructor argument:

List<String> keyList = new ArrayList<String>(map.keySet());
List<String> valueList = new ArrayList<String>(map.values());