How to convert Map to List in java

You are here :
Home / Core Java Tutorials / Collection framework Tutorial in java

In this tutorial we will learn How to convert Map to List in Java with program and examples

In projects you must have face many scenarios where you need to convert  keys in map to List or to convert to keys in map to List in Java. In this tutorial there are program and example which can help you solve this issue in java.

How to convert ArrayList to HashSet and Set to Array in Java examples




  • Program/Example 1.1 to convert keys in map to ArrayList in java 7 or before
  • Program/Example 1.2 to convert keys in map to ArrayList in java 7 or before


  • Program/Example 2.1 to convert keys in map to ArrayList in java 8
  • Program/Example 2.2 to convert keys in map to ArrayList in java 8



1. How to convert  Map to List in java 7 or before>

Program/Example 1.1 to convert keys in map to ArrayList in java 7 or before

map.keySet() method returns a Set which contains all the keys in the map.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertMapKeysToListExample {
   public static void main(String[] args) {
          Map<Integer, String> map = new HashMap<Integer, String>();
          map.put(1, "audi");
          map.put(2, "bmw");
          map.put(3, "ferrari");
         
          //convert keys in map to ArrayList
          //by passing keys in map in constructor of ArrayList
          List<Integer> arrayList = new ArrayList<Integer>(map.keySet());
          System.out.println("arrayList containing keys in map = "+arrayList);   
   }
}
/*OUTPUT
arrayList containing keys in map = [1, 2, 3]
*/

Program/Example 1.2 to convert keys in map to ArrayList in java 7 or before

map.values() method returns a Collection which contains all the values in the map.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertMapValuesToListExample {
   public static void main(String[] args) {
          Map<Integer, String> map = new HashMap<Integer, String>();
          map.put(1, "audi");
          map.put(2, "bmw");
          map.put(3, "ferrari");
         
          //convert values in map to ArrayList
          //by passing values in map in constructor of ArrayList
          List<String> arrayList = new ArrayList<String>(map.values());
          System.out.println("arrayList containing values in map = "+arrayList);        
   }
}
/*OUTPUT
arrayList containing values in map = [audi, bmw, ferrari]
*/

2. How to convert  Map to List in java 8 >

Program/Example 2.1 to convert keys in map to ArrayList in java 8

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ConvertMapKeysToListJava8Example {
   public static void main(String[] args) {
          Map<Integer, String> map = new HashMap<Integer, String>();
          map.put(1, "audi");
          map.put(2, "bmw");
          map.put(3, "ferrari");
         
          //convert keys in map to ArrayList in java 8 using streams
          List<Object> list = map.keySet().stream().collect(Collectors.toList());
          System.out.println("arrayList containing keys in map = "+list);
         
   }
}
/*OUTPUT
arrayList containing keys in map = [1, 2, 3]
*/


Program/Example 2.2 to convert keys in map to ArrayList in java 8

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ConvertMapValuesToListJava8Example {
   public static void main(String[] args) {
          Map<Integer, String> map = new HashMap<Integer, String>();
          map.put(1, "audi");
          map.put(2, "bmw");
          map.put(3, "ferrari");
         
          //convert keys in values to ArrayList in java 8 using streams
          List<Object> list = map.values().stream().collect(Collectors.toList());
          System.out.println("arrayList containing values in map = "+list);
         
   }
}
/*OUTPUT
arrayList containing keys in map = [1, 2, 3]
*/



In this Collection framework tutorial we discussed advantage of using Vector in multithreading environment in java with program and examples.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

How to convert ArrayList to HashSet and Set to Array in Java examples

convert Array to Set and Set to array in Java

How to convert Array to List and ArrayList to array in Java



Collection - List, Set and Map all properties in tabular form




Collection - List, Set and Map all properties in tabular form




eEdit
Must read for you :