How to convert a Map to List in Java?

JavaListCollectionsMap

Java Problem Overview


What is the best way to convert a Map<key,value> to a List<value>? Just iterate over all values and insert them in a list or am I overlooking something?

Java Solutions


Solution 1 - Java

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

assuming:

Map<Key,Value> map;

Solution 2 - Java

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

Solution 3 - Java

Using the Java 8 Streams API.

List<Value> values = map.values().stream().collect(Collectors.toList());

Solution 4 - Java

map.entrySet() gives you a collection of Map.Entry objects containing both key and value. you can then transform this into any collection object you like, such as new ArrayList(map.entrySet());

Solution 5 - Java

a list of what ?

Assuming map is your instance of Map

  • map.values() will return a Collection containing all of the map's values.
  • map.keySet() will return a Set containing all of the map's keys.

Solution 6 - Java

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());

Solution 7 - Java

You can do it like this

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

Solution 8 - Java

    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.

Solution 9 - Java

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).

Solution 10 - Java

// you can use this
List<Value> list = new ArrayList<Value>(map.values());

// or you may use 
List<Value> list = new ArrayList<Value>();
for (Map.Entry<String, String> entry : map.entrySet())
{
list.add(entry.getValue());    
}

Solution 11 - Java

 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());
 }

Solution 12 - Java

HashMap<Integer, List<String>> map = new HashMap<>(); 
List<String> list = new ArrayList<String>();
list.add("Java");
list.add("Primefaces");
list.add("JSF");
map.put(1,list);
if(map != null){
	return new ArrayList<String>((Collection<? extends String>) map.values());
}

Solution 13 - Java

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;
}

Solution 14 - Java

If you want an immutable copy of the values:

List<Value> list = List.copyOf(map.values())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavaaView Question on Stackoverflow
Solution 1 - JavacletusView Answer on Stackoverflow
Solution 2 - JavacoobirdView Answer on Stackoverflow
Solution 3 - JavaMatej KormuthView Answer on Stackoverflow
Solution 4 - Javajava dudeView Answer on Stackoverflow
Solution 5 - JavaDiego AmicabileView Answer on Stackoverflow
Solution 6 - JavamaneeshView Answer on Stackoverflow
Solution 7 - Javauser3752441View Answer on Stackoverflow
Solution 8 - Javauser11View Answer on Stackoverflow
Solution 9 - JavaM0lesView Answer on Stackoverflow
Solution 10 - Javasaurabhgoyal795View Answer on Stackoverflow
Solution 11 - Javasiva prasadView Answer on Stackoverflow
Solution 12 - JavaHakan AnlamazView Answer on Stackoverflow
Solution 13 - JavaHiral PancholiView Answer on Stackoverflow
Solution 14 - JavaStefan HaberlView Answer on Stackoverflow