How do I convert a Map<key,value> to a List<value>? Should I iterate over all map values and insert them into a list?
-
1I think it's not a good model if one has to convert map into list. One should write the code in such manner that this situation doesn't arrise.Bugs Happen– Bugs Happen2017-12-05 17:13:01 +00:00Commented Dec 5, 2017 at 17:13
13 Answers
List<Value> list = new ArrayList<Value>(map.values());
assuming:
Map<Key,Value> map;
8 Comments
Collection to List would work.keySet() and values() are generally shim objects that give a Set or Collection view of the underlying structure (keySet() returns a Set to emphasize no dupes). For values(), the returned object may be a List, but often won't be. Creating a real List, as you say, breaks the link which means you're no longer dependent on the original Map. Sometimes though, you only need a List because some API requires one - reinforcing the rule that a good API should require only the most abstract interfaces it can get away with...The issue here is that Map has two values (a key and value), while a List only has one value (an element).
Therefore, the best that can be done is to either get a List of the keys or the values. (Unless we make a wrapper to hold on to the key/value pair).
Say we have a Map:
Map<String, String> m = new HashMap<String, String>();
m.put("Hello", "World");
m.put("Apple", "3.14");
m.put("Another", "Element");
The keys as a List can be obtained by creating a new ArrayList from a Set returned by the Map.keySet method:
List<String> list = new ArrayList<String>(m.keySet());
While the values as a List can be obtained creating a new ArrayList from a Collection returned by the Map.values method:
List<String> list = new ArrayList<String>(m.values());
The result of getting the List of keys:
Apple Another Hello
The result of getting the List of values:
3.14 Element World
3 Comments
HashMap and similar un-sorted Map implementations it will be effectively random.Using the Java 8 Streams API.
List<Value> values = map.values().stream().collect(Collectors.toList());
4 Comments
java List<Value> values = new ArrayList<>(map.values()); I guess you want to convert the values contained in the Map to a list? Easiest is to call the values() method of the Map interface. This will return the Collection of value objects contained in the Map.
Note that this Collection is backed by the Map object and any changes to the Map object will reflect here. So if you want a separate copy not bound to your Map object, simply create a new List object like an ArrayList passing the value Collection as below.
ArrayList<String> list = new ArrayList<String>(map.values());
Comments
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("java", 20);
map.put("C++", 45);
Set <Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
we can have both key and value pair in list.Also can get key and value using Map.Entry by iterating over list.
Comments
If you want to ensure the values in the resultant List<Value> are in the key-ordering of the input Map<Key, Value>, you need to "go via" SortedMap somehow.
Either start with a concrete SortedMap implementation (Such as TreeMap) or insert your input Map into a SortedMap before converting that to List. e.g.:
Map<Key,Value> map;
List<Value> list = new ArrayList<Value>( new TreeMap<Key Value>( map ));
Otherwise you'll get whatever native ordering the Map implementation provides, which can often be something other than the natural key ordering (Try Hashtable or ConcurrentHashMap, for variety).
Comments
Map<String, String > map = new HapshMap<String, String>;
map.add("one","java");
map.add("two", "spring");
Set<Entry<String, String>> set = map.entrySet();
List<Entry<String, String>> list = new ArrayList<Entry<String, String>> (set);
for(Entry<String, String> entry : list) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
Comments
public List<Object> convertMapToList(Map<Object, Object> map){
return new ArrayList<>(map.values());
}
2 Comments
Here's the generic method to get values from map.
public static <T> List<T> ValueListFromMap(HashMap<String, T> map) {
List<T> thingList = new ArrayList<>();
for (Map.Entry<String, T> entry : map.entrySet()) {
thingList.add(entry.getValue());
}
return thingList;
}
1 Comment
If you want an immutable copy of the values:
List<Value> list = List.copyOf(map.values())
3 Comments
List.copyOf?java.util.List?