Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple maps from a list in Java 8

Tags:

java

Is there a way to create multiple maps from a list in Java 8 using stream or other way?

I have a list of custom object. Suppose List where Employee has departmentId so there are some employees which have the same departmentId = "001" for example. I want to create multiple maps based on Employee.departmentId.

For example,

map1 = <"001", employee1>,<"001", employee3>, <"001", employee8>...<"001", employee100>

map2 = <"002", employee2>,<"002", employee4>,..., <"002", employee8> ... something like this.

since for each element in list check the departmentId and put it in corresponding map by using old for loop. Wondering is there a better way to do this? Currently I'm using Java 8

like image 223
user2761895 Avatar asked Oct 21 '25 12:10

user2761895


1 Answers

As has been suggested, using a Map<String,List<Employee>> is probably what you want. I constructed a small class and an example to illustrate the process.

      List<Employee> emps = List.of(
            new Employee("001", "Jim"),
            new Employee("002", "Bob"),
            new Employee("001", "June"),
            new Employee("002", "Jean"));

     // For each employee, using departmentId as the key, construct
     // a map containing lists of all employees that have that ID

      Map<String, List<Employee>> empMap = 
          emps
              .stream()
              .collect(
                  Collectors.groupingBy(emp -> emp.departmentId)
              );

      for (Entry<?, ?> e : empMap.entrySet()) {
         System.out.println(e.getKey() + "-->" + e.getValue());
      }



      class Employee {
          public String departmentId;
          public String name;
    
          public Employee(String departmentId, String name) {
             this.departmentId = departmentId;
             this.name = name;
          }
          public String toString() {
             return "(" + departmentId + " " + name + ")";
          }
       }

This displays

001-->[(001 Jim), (001 June)]
002-->[(002 Bob), (002 Jean)]

Another option, which would do the exact same thing is as follows:

This creates an empty map. Then it iterates over the list of employees and computes whether the value for the key exists. If it does, it returns the list and adds the entry to that list. If it doesn't, it then creates the new ArrayList and then adds the employee for that list, storing the list in the provided key.

     Map<String, List<Employee>> emp2 = new HashMap<>();
         for (Employee emp : emps) {
            emp2.compute(emp.departmentId,
               (ID, list) -> list == null ? new ArrayList<>()
                     : list).add(emp);
      }

In this case my preference would be the first method.

like image 124
WJS Avatar answered Oct 23 '25 01:10

WJS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!