How to Convert a List Into Map of Map Map<String, Map<String,Object>>

ps_403 :

I have a List<Employee> e which I want to convert into Map<String, Map<String,Emp>> where the outer string should be "Name" and the inner string should be "Domain".

       Name Id Domain
e(0) - Emp1, 1, Insurance
e(1) - Emp1, 2, Sales
e(2) - Emp2, 3, Sales
e(3) - Emp4, 4, Marketing

I am tried the following-

e.stream().collect(Collectors.groupingBy(
                                   Employee::getName,
                                   toMap(Employee::getDomain,Emp)));

So the expected output map should look like

<Emp1>
     <Insurance, e(0)>
     <Sales, e(1)>
<Emp2>
     <Sales, e(2)>
<Emp4>
     <Marketing, e(3)>

But I get only unique values, actual output-

<Emp1>
     <Insurance, e(0)>
<Emp2>
     <Sales, e(2)>
<Emp4>
     <Marketing, e(3)>

Can someone tell the best way to do it?

Naman :

What you are mostly looking for is nested grouping such as :

Map<String, Map<String, List<Employee>>> groupedMap = employees.stream()
        .collect(Collectors.groupingBy(Employee::getName,
                Collectors.groupingBy(Employee::getDomain, Collectors.toList())));

Note - The values are List<Employee> which are the employees grouped by name and then by domain. (Both same clubbed into a List.)


If you were to stricly adhere to getting a single employee corresponding to the specified grouping, the code pretty much works for me with a small modification:

Map<String, Map<String, Employee>> groupedReducedMap = employees.stream()
        .collect(Collectors.groupingBy(Employee::getName,
                Collectors.toMap(Employee::getDomain,
                        Function.identity(), // value as the employee instance
                        (a, b) -> a))); // choose first instance for similar 'domain'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java 8: How to convert List<String> to Map<String,List<String>>?

Map JSON To List<Map<<String, Object>>

Convert String to List of Map

convert map string string to map string list string - Java 7

Convert Map<String,Object> to Map<String,String>

Java: how to convert a List<?> to a Map<String,?>

How to convert map of map of interface to string

How to Convert List<Map<String, Object>> to List<Map<String, String>>

Convert Map<Integer, List<Object>> to Map<Integer, Map<String, Map<LocalDate, Integer>>> using Java stream API

How to add Map<String, string> into Map<String, List<Object>> in java

Convert List<Map<String, Object>> to String[][]

How to group List<Map<String,Object>> to Map<String,List<Map<String,Object>> in Java8

Convert List<Map<String, Object>> to Map<String, List<Map<String, Object>>> using Java 8 streams API

Convert Map<String,List<String>> to List<Map<String,String>> in Kotlin

How to convert Object list into a map of <String, List<String>>?

How to convert list map to json string and json to list map

Convert Map<String, Object> to Map<String, List<Object>>

Convert byte[] to List<Map<String, Object>>

How to convert Map<String, Object> to Map<Key, Value> in dart?

How to convert a list of string to map in Scala?

How to convert List<Map<String,Object>> to Map<String, String>?

Reduce map(map(map(list(string)))) terraform

How to convert a list of java objects to Map<String, Map<String, String>>

How to convert list to Map of Map

how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>>

How to Convert a Map<String, List<List<String>>> to Map<String, List<String>>

How convert object list into map?

How to convert Map<String, Map<String, String>> to String?

how to convert List<Map<String, String>> to Map<String, List<String>>