Home > database >  convert List<object> to map
convert List<object> to map

Time:12-20

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", "[email protected]");
Employee emp2 = new Employee("2", "Mahesh", "Trainer", "[email protected]");
Employee emp3 = new Employee("3", "Vipul", "SEO", "[email protected]");
Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "[email protected]");
Employee emp5 = new Employee("5", "Rakesh", "Marketing", "[email protected]");

//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?

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

You should/may do like this:

Employee emp1 = new Employee("1","Mayank","HR","[email protected]");
    Employee emp2 = new Employee("2","Mahesh","Trainer","[email protected]");
    Employee emp3 = new Employee("3","Vipul","SEO","[email protected]");
    Employee emp4 = new Employee("4","Ajay","Devlopwr","[email protected]");
    Employee emp5 = new Employee("5","Rakesh","Marketing","[email protected]");
        
         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);
         }

CodePudding user response:

Hard-code a 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", "[email protected]");
Employee emp2 = new Employee("2", "Mahesh", "Trainer", "[email protected]");
Employee emp3 = new Employee("3", "Vipul", "SEO", "[email protected]");
Employee emp4 = new Employee("4", "Ajay", "Devlopwr", "[email protected]");
Employee emp5 = new Employee("5", "Rakesh", "Marketing", "[email protected]");

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", "[email protected]") ,
        new Employee("2", "Mahesh", "Trainer", "[email protected]") ,
        new Employee("3", "Vipul", "SEO", "[email protected]") ,
        new Employee("4", "Ajay", "Devlopwr", "[email protected]") ,
        new Employee("5", "Rakesh", "Marketing", "[email protected]")
    );

From that list, build your map.

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 elements of mapEmpIdToEmp.

  •  Tags:  
  • java
  • Related