I have an Employee
class that contain four fields: id, name, department, and email. I have created five objects, set the values and added all of them to a list.
Now, I want to convert that list to map but I am not able to. For your reference, here is the code that I tried
Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com");
Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com");
Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com");
Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com");
Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com");
//add class object to list
List < Employee > listWmp = new ArrayList < > ();
listWmp.add(0, emp1);
listWmp.add(1, emp2);
listWmp.add(2, emp3);
listWmp.add(3, emp4);
listWmp.add(4, emp5);
System.out.println("list elements are : " + listWmp);
//convert the list into map
Map < String, Employee > listMap = new HashMap < > ();
for (Employee employee: listWmp) {
listMap.put(employee.getEmpId(), employee.getEmpBame());
}
How do I convert the list to a map?
The best way is to use a Stream and then collect the employee:
Map<String, Employee> listMap = listWmp.stream()
.collect(Collectors.toMap(Employee::getEmpId, employee -> employee));
Or if you don't want to use Java Stream API you have to change your code to:
listMap.put(employee.getEmpId(), employee);
A
Map
is an object that maps keys to values.
Your Map
is defined as Map<String, Employee>
. So it maps a String
to an Employee
.
When you try to execute:
listMap.put(employee.getEmpId(), employee.getEmpBame());
It fails because the second parameter is supposed to be an instance of Employee
but employee.getEmpBame()
returns a String
.
You should replace it with:
listMap.put(employee.getEmpId(), employee);
You should/may do like this:
Employee emp1 = new Employee("1","Mayank","HR","mayank@gmail.com");
Employee emp2 = new Employee("2","Mahesh","Trainer","Mahesh@gmail.com");
Employee emp3 = new Employee("3","Vipul","SEO","Vipul@gmail.com");
Employee emp4 = new Employee("4","Ajay","Devlopwr","Ajay@gmail.com");
Employee emp5 = new Employee("5","Rakesh","Marketing","Rakesh@gmail.com");
List<Employee> listWmp = new ArrayList<>();
listWmp.add(emp1);
listWmp.add(emp2);
listWmp.add(emp3);
listWmp.add(emp4);
listWmp.add(emp5);
Map<String, Employee> listMap = new HashMap<>();
for (Employee employee : listWmp) {
listMap.put(employee.getEmpId(), employee);
}
Map
As commented , in your particular example you could simply hard-code a Map
rather than hard-code a List
.
Employee emp1 = new Employee("1", "Mayank", "HR", "mayank@gmail.com");
Employee emp2 = new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com");
Employee emp3 = new Employee("3", "Vipul", "SEO", "Vipul@gmail.com");
Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com");
Employee emp5 = new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com");
Map < String , Employee > mapEmpIdToEmp = new HashMap < > ();
mapEmpIdToEmp.put( emp1.id() , emp1 );
mapEmpIdToEmp.put( emp2.id() , emp2 );
mapEmpIdToEmp.put( emp3.id() , emp3 );
mapEmpIdToEmp.put( emp4.id() , emp4 );
mapEmpIdToEmp.put( emp5.id() , emp5 );
Map.of
The Map.of
method in modern Java makes literal map instantiation quite easy and readable. The resulting Map
is unmodifiable .
Map < String , Employee > mapEmpIdToEmp =
Map.of(
emp1.id() , emp1 , // key , value
emp2.id() , emp2 ,
emp3.id() , emp3 ,
emp4.id() , emp4 ,
emp5.id() , emp5
);
List.of
Indeed, you could use List.of
for hard-coding those 5 new Employee
objects into an unmodifiable List
.
List< Employee > employees =
List.of(
new Employee("1", "Mayank", "HR", "mayank@gmail.com") ,
new Employee("2", "Mahesh", "Trainer", "Mahesh@gmail.com") ,
new Employee("3", "Vipul", "SEO", "Vipul@gmail.com") ,
new Employee("4", "Ajay", "Devlopwr", "Ajay@gmail.com") ,
new Employee("5", "Rakesh", "Marketing", "Rakesh@gmail.com")
);
From that list, build your map in a soft-coded manner.
Map < String , Employee > mapEmpIdToEmp = new HashMap < > ();
for( Employee employee : employees )
{
mapEmpIdToEmp.put( employee.id() , employee ) ;
}
If desired, call Map.copyOf
to make an unmodifiable map from mapEmpIdToEmp
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.