Java 8 Convert Map to List using Collectors.toList() Example

By Arvind Rai, September 23, 2016
On this page we will provide java 8 convert Map to List using Collectors.toList() example. A Map has key and value and we can get all keys and values as List. If we want to set key and value in a class attribute and then add the object into List, we can achieve it in single line of code of java 8 using Collectors.toList(). Now let us see how to do it.

Map to List with Lambda Expression

To convert Map to List with lambda expression using Collectors.toList() is as follows.
List<String> valueList = map.values().stream().collect(Collectors.toList()); 
If we want to sort the values before putting into List, we do it as follows.
List<String> sortedValueList = map.values().stream()
			.sorted().collect(Collectors.toList()); 
We can also convert Map to List of user object with given Comparator using Comparator.comparing().
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList()); 
Here Person is a user class. We can also use Map.Entry to get Map value and key as follows.
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList()); 
For comparison, we can also use Map.Entry.comparingByValue() and Map.Entry.comparingByKey() to sort the data on the basis of value and key respectively.
List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
	.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList()); 

Simple Map to List Example


SimpleMapToList.java
package com.concretepage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SimpleMapToList {
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(23, "Mahesh");
		map.put(10, "Suresh");
		map.put(26, "Dinesh");
		map.put(11, "Kamlesh");
		System.out.println("--Convert Map Values to List--");
		List<String> valueList = map.values().stream().collect(Collectors.toList());
		valueList.forEach(n -> System.out.println(n));
		
		System.out.println("--Convert Map Values to List using sort--");
		List<String> sortedValueList = map.values().stream()
				.sorted().collect(Collectors.toList());
		sortedValueList.forEach(n -> System.out.println(n));		
		
		System.out.println("--Convert Map keys to List--");
		List<Integer> keyList = map.keySet().stream().collect(Collectors.toList());
		keyList.forEach(n -> System.out.println(n));
		
		System.out.println("--Convert Map keys to List using sort--");
		List<Integer> sortedKeyList = map.keySet().stream()
				.sorted().collect(Collectors.toList());
		sortedKeyList.forEach(n -> System.out.println(n));
	}
} 
Output
--Convert Map Values to List--
Mahesh
Suresh
Dinesh
Kamlesh
--Convert Map Values to List using sort--
Dinesh
Kamlesh
Mahesh
Suresh
--Convert Map keys to List--
23
10
26
11
--Convert Map keys to List using sort--
10
11
23
26 

Convert Map to List of User Object Example


MapToListOfUserObject
package com.concretepage;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MapToListOfUserObject {
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(23, "Mahesh");
		map.put(10, "Suresh");
		map.put(26, "Dinesh");
		map.put(11, "Kamlesh");
		
		List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
				.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
		
		list.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName()));		
	}
} 
Person.java
package com.concretepage;
public class Person {
	private Integer id;
	private String name;
	public Person(Integer id, String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public String getName() {
		return name;
	}
} 
Output
Id: 10, Name: Suresh
Id: 11, Name: Kamlesh
Id: 23, Name: Mahesh
Id: 26, Name: Dinesh 
Here the list has been sorted by Map key and if we want to sort it by value, we need to use
Comparator.comparing(e -> e.getValue())
and then output will be as follows.
Id: 26, Name: Dinesh
Id: 11, Name: Kamlesh
Id: 23, Name: Mahesh
Id: 10, Name: Suresh 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us