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.
Also read : convert Array to Set and Set to array in Java
How to convert ArrayList to HashSet and Set to Array in Java examples
1. How to convert Map to List in java 7 or before>
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]
*/
|
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 >
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]
*/
|
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
Labels:
Core Java
Core java Conversions