List map to list java 8
A Map is an object that maps keys to values or is a collection of attribute-value pairs. The list is an ordered collection of objects and the List can contain duplicate values. The Map has two values (a key and value), while a List only has one value (an element). So we can generate two lists as listed:
Let us assume “map” is the instance of Map, henceforth as usual we all know it contains set and value pairs. so they are defined as follows:
Methods:
Let us discuss each of them Methods 1: Passing sets of keys inside ArrayList constructor parameter Procedure: We can convert Map keys to List of Keys bypassing set of map keys generated by map.keySet() method to the ArrayList constructor parameter as shown below map.values(); // Now here e will pass sets of keys of our map, so it becomes map.values(map.keySet()); // Now we just need to store it into List, so creating object List ListofKeys = new ArrayList(map.keySet()); // Now we are done with conversion.Syntax: Henceforth it as follows: List ListofKeys = new ArrayList(map.keySet());Method 2: List ListofKeys = new ArrayList(map.keySet()); We can convert Map keys to a List of Values by passing a collection of map values generated by map.values() method to ArrayList constructor parameter. Syntax: Henceforth it is as follows: List Listofvalues = new ArrayList(map.values());Example
Output: 1 2 3 4 5 6Method 3: Using Streams API The stream() method returns a stream of the keys from the Set of the map keys returned by Map.keySet(). The collect() method of the Stream class collects the stream of keys in a List.The Collectors.toCollection(ArrayList::new) passed to the collect() method to collect as new ArrayList. One can also use Stream API to convert map keys and values to respective lists. the syntax is as provided below as follows: List ListofKeys = map.keyset().stream().collect(Collectors.toCollection(ArrayList::new));List Listofvalues= map.values().stream().collect(Collectors.toCollection(ArrayList::new));
Implementation: Given RollNo and Student Name of N students as input. First, we create a Map where Rollno is key because Rollno is unique and Name as Value for Map then convert this map to list of values and keys respectively. Where the generated list of keys contains RollNo of students and list of Values contains Name of Students. Example
Output:
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.ContentsTo convert Map to List with lambda expression using Collectors.toList() is as follows.
List
List
List
List
List 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 --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 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 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; } } OutputId: 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 useComparator.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 |