In Java, how does one get the values of a HashMap
returned as a List
?
If you wanna maintain the same order in your list, say: your Map looks like:
map.put(1, "msg1")
map.put(2, "msg2")
map.put(3, "msg3")
and you want your list looks like
["msg1", "msg2", "msg3"] // same order as the map
you will have to iterate through the Map:
// sort your map based on key, otherwise you will get IndexOutofBoundException
Map treeMap = new TreeMap(map)
List list = new List();
for (treeMap.Entry entry : treeMap.entrySet()) {
list.add(entry.getKey(), entry.getValue());
}