In this tutorial, we will see “How to convert List to Map using Java 8?”How to convert List to Map in java 8
Java 8 – How to convert List to Map? Share on X
/**
* Using Java 8, Convert a List to Map
* @author Deepak Verma
*
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class ListToMap {
private int id;
private String name;
private long players;
public ListToMap(int id, String name, long players) {
this.id = id;
this.name = name;
this.players = players;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getplayers() {
return players;
}
public void setplayers(long players) {
this.players = players;
}
public static void main(String[] args) {
List<ListToMap> listOfPlayers = new ArrayList<>();
listOfPlayers.add(new ListToMap(1, "Rohit Sharma", 45));
listOfPlayers.add(new ListToMap(2, "David Warner", 72));
listOfPlayers.add(new ListToMap(3, "MSD", 30));
listOfPlayers.add(new ListToMap(4, "Faf du Plessis", 84));
listOfPlayers.add(new ListToMap(5, "Shikhar Dhawan", 99));
listOfPlayers.add(new ListToMap(6, "Jos Butler", 61));
listOfPlayers.add(new ListToMap(7, "Shubhman Gill", 49));
listOfPlayers.add(new ListToMap(8, "Devon Convay", 91));
listOfPlayers.add(new ListToMap(9, "Virat Kohli", 79));
listOfPlayers.add(new ListToMap(10, "Glenn Maxwell", 55));
//where key is id
Map<Integer, String> result1 = listOfPlayers.stream().collect(
Collectors.toMap(ListToMap::getId, ListToMap::getName));
System.out.println("Result if Key is Id : " + result1);
//where key is name
Map<String, Long> result2 = listOfPlayers.stream().collect(
Collectors.toMap(ListToMap::getName, ListToMap::getplayers));
System.out.println("Result if Key is name : " + result2);
}
}
Output:
Result if Key is Id : {1=Rohit Sharma, 2=David Warner, 3=MSD, 4=Faf du Plessis, 5=Shikhar Dhawan, 6=Jos Butler, 7=Shubhman Gill, 8=Devon Convay, 9=Virat Kohli, 10=Nicholas Pooran}
Result if Key is name : {Faf du Plessis=84, Shubhman Gill=49, Virat Kohli=79, Rohit Sharma=45, Jos Butler=61, Devon Convay=91, David Warner=72, Shikhar Dhawan=99, Nicholas Pooran=55, MSD=30}
How to convert List to Map in java 8
Java 8 – How to convert List to Map? Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: