猿问

如何将列表转换为 Map 的 Map Map<String, Map<String,Object>>

我有一个List<Employee>e,我想将其转换为 Map<String, Map<String,Emp>> 外部字符串应为“Name”而内部字符串应为“Domain”的位置。


       Name Id Domain

e(0) - Emp1, 1, Insurance

e(1) - Emp1, 2, Sales

e(2) - Emp2, 3, Sales

e(3) - Emp4, 4, Marketing

我尝试了以下方法-


e.stream().collect(Collectors.groupingBy(

                                   Employee::getName,

                                   toMap(Employee::getDomain,Emp)));

所以预期的输出图应该是这样的


<Emp1>

     <Insurance, e(0)>

     <Sales, e(1)>

<Emp2>

     <Sales, e(2)>

<Emp4>

     <Marketing, e(3)>

但我只得到独特的值,实际输出-


<Emp1>

     <Insurance, e(0)>

<Emp2>

     <Sales, e(2)>

<Emp4>

     <Marketing, e(3)>

有人可以告诉最好的方法吗?


FFIVE
浏览 141回答 2
2回答

小唯快跑啊

您最需要寻找的是嵌套分组,例如:Map<String, Map<String, List<Employee>>> groupedMap = employees.stream()&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Employee::getName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(Employee::getDomain, Collectors.toList())));注- 这些值是List<Employee>按姓名分组的员工,然后按域分组的员工。(两者都同样加入到一个列表中。)如果您严格遵守让单个员工与指定的分组相对应,那么只需稍作修改,代码就对我来说非常有效:Map<String, Map<String, Employee>> groupedReducedMap = employees.stream()&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Employee::getName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.toMap(Employee::getDomain,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function.identity(), // value as the employee instance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (a, b) -> a))); // choose first instance for similar 'domain'

慕神8447489

由于输出应该是Map<String, Map<String,Employee>>而不是Map<String, Map<String,List<Employee>>>(即根据您请求的输出,不能有两个Employee具有相同名称和相同域的 s),您可以链接两个groupingBy,然后使用它reducing来确保每个内部组将有一个Employee而不是一个List<Employee>:Map<String, Map<String,Optional<Employee>>> output =&nbsp; &nbsp; e.stream()&nbsp; &nbsp; &nbsp;.collect(Collectors.groupingBy(Employee::getName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(Employee::getDomain,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.reducing((x1,x2)->x2))));这个版本的问题reducing是它返回一个Optional<Employee>而不是Employee,即使我们知道Optional永远不会为空。我们可以通过以下方式解决这个问题:&nbsp; Map<String, Map<String,Employee>> output =&nbsp; &nbsp; &nbsp; e.stream()&nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.groupingBy(Employee::getName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(Employee::getDomain,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.reducing(e.get(0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (x1,x2)->x2))));现在,我们使用具有标识值的变体reducing,向其传递任意Employee实例(哪个实例并不重要,因为它总是会被正确的实例替换)。
随时随地看视频慕课网APP

相关分类

Java
我要回答