How to Convert a Map to a List in Java Example

Map and List are two common data structures available in Java

The main difference between Map (HashMap, ConcurrentHashMap or TreeMap) and List is that Map holds two objects key and value while List just holds one object which itself is a value

How to Convert Map into List in Java with Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

public class JavamapToList {
    
    static boolean result;
    public static void main(String[] args)
    {
      
      HashMap<String, String> javaMaps= new HashMap<String, String>();
        // preparing hashmap with keys and values
        javaMaps.put("JAVA1", "JAVA 1");
        javaMaps.put("JAVA2", "Core Java");
        javaMaps.put("Java3", "JEE");
        javaMaps.put("Java4", "Spring");
      
        System.out.println("Size of javaMapsMap: " + javaMaps.size());
      
        //Converting HashMap keys into ArrayList
        List<String> keyList = new ArrayList<String>(javaMaps.keySet());
        System.out.println("Size of Key list from Map: " + keyList.size());
      
        //Converting HashMap Values into ArrayList
        List<String> valueList = new ArrayList<String>(javaMaps.values());
        System.out.println("Size of Value list from Map: " + valueList.size());


      
        List<Entry> entryList = new ArrayList<Entry>(javaMaps.entrySet());
        System.out.println("Size of Entry list from Map: " + entryList.size());

    }
}

Output

Size of javaMapsMap: 4
Size of Key list from Map: 4
Size of Value list from Map: 4
Size of Entry list from Map: 4