Skip to content

Instantly share code, notes, and snippets.

@adityachaudhari
Created February 21, 2017 21:57
Show Gist options
  • Save adityachaudhari/b48cfe5bdd2a7c88962e67a2b2eea188 to your computer and use it in GitHub Desktop.
Save adityachaudhari/b48cfe5bdd2a7c88962e67a2b2eea188 to your computer and use it in GitHub Desktop.
sample
package com.examples.java8.ListTomap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by adityacchaudhari
*/
public class ListToMapExample
{
public static void main(String[] args)
{
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(1001, "Steive", "[email protected]", "Marketing"));
employeeList.add(new Employee(1002, "Joe", "[email protected]", "Admin"));
employeeList.add(new Employee(1003, "louise", "[email protected]", "Marketing"));
employeeList.add(new Employee(1004, "karan", "[email protected]", "Admin"));
//create map of ID and Email ID by accessing method way
Map<Integer,String> mapOfIDAndEmail = employeeList.stream().collect(Collectors.toMap(Employee::getID, Employee::getEmailID));
//create map of ID and Email ID by predicate way
Map<Integer,String> mapOfIDAndEmialPredicateWay = employeeList.stream()
.collect(Collectors.toMap(ID -> ID.getID(), emailID -> emailID.getEmailID()));
System.out.println("The mapOfIdAndEmail is : " + mapOfIDAndEmail);
System.out.println("The mapOfIDAndEmialPredicateWay is : " + mapOfIDAndEmialPredicateWay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment