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:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts