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? Click To Tweet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /** * 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:
1 2 | 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? Click To Tweet
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: