HashMap:多个值作为列表

huangapple go评论71阅读模式
英文:

HashMap: multiple values as list

问题

以下是您提供的代码的中文翻译:

我有如下所示的列表

List<Employees> lt = new ArrayList<Employees>();
lt.add(new Employees("111", "Grade1")); //111 是员工编号
lt.add(new Employees("222", "Grade2"));
lt.add(new Employees("333", "Grade2"));
lt.add(new Employees("444", "Grade3"));
lt.add(new Employees("555", "Grade3"));
lt.add(new Employees("666", "Grade1"));
..............................
我试图使用Java 8 的帮助尽量少的代码从列表 lt 中创建如下所示的映射
这里 Grade 是员工的职务所以对于特定的职务我需要找出员工编号的列表

Map<String, List<String>> dataMap = new HashMap<>();
dataMap.put("Grade1", List1);  列表包含 "111""666"
dataMap.put("Grade2", List2);  列表包含 "222""333"
dataMap.put("Grade3", List3);  列表包含 "444""555"

...............................
提前感谢您提供的任何帮助

希望这能帮助到您。如果您有任何其他翻译需求,请随时告诉我。

英文:

I have the list as mentioned below.

        List&lt;Employees&gt; lt = new ArrayList&lt;Employees&gt;(); 
		lt.add(new Employees(&quot;111&quot;, &quot;Grade1&quot;)); //111 is the employee id
        lt.add(new Employees(&quot;222&quot;, &quot;Grade2&quot;)); 
        lt.add(new Employees(&quot;333&quot;, &quot;Grade2&quot;));
		lt.add(new Employees(&quot;444&quot;, &quot;Grade3&quot;));
		lt.add(new Employees(&quot;555&quot;, &quot;Grade3&quot;));
		lt.add(new Employees(&quot;666&quot;, &quot;Grade1&quot;));
        ..............................

Am trying to create a map as mentioned below with the help of java8 with minimum coding from the list lt .
Here Grade is the designation of the employee. So for a particular grade I have to find out list of employee ids.

Map&lt;String, List&lt;String&gt;&gt; dataMap= new HashMap&lt;&gt;();
dataMap.put(&quot;Grade1&quot;, List1);  List contains &quot;111&quot; and &quot;666&quot;
dataMap.put(&quot;Grade2&quot;, List2);  List contains &quot;222&quot; and &quot;333&quot;
dataMap.put(&quot;Grade3&quot;, List3);  List contains &quot;444&quot; and &quot;555&quot;

...............................

Thank you in advance for any help you can provide

答案1

得分: 4

你可以使用Collectors.groupingBy收集器来完成这个任务。假设你的Employees类有getGradegetEmployeeID方法,你可以这样写:

Map<String, List<String>> dataMap = lt.stream().collect(Collectors.groupingBy(
    Employees::getGrade,
    LinkedHashMap::new,
    Collectors.mapping(Employees::getEmployeeID, Collectors.toList())));

输出结果是一个Map,内容如下:

{Grade1=[111, 666], Grade2=[222, 333], Grade3=[444, 555]}
英文:

You can do this using the Collectors.groupingBy collector. Assuming that your Employees class has getGrade and getEmployeeID methods, you can write:

    Map&lt;String, List&lt;String&gt;&gt; dataMap = lt.stream().collect(Collectors.groupingBy(
        Employees::getGrade,
        LinkedHashMap::new,
        Collectors.mapping(Employees::getEmployeeID, Collectors.toList())));

The output is a Map with:

{Grade1=[111, 666], Grade2=[222, 333], Grade3=[444, 555]}

答案2

得分: 1

你不需要地图。你可以使用流来做到这一点。要获取Grade1,你可以这样说:

List<Employees> grade1Employees = lt.stream().filter(e -> e.getGrade().equals("Grade1")).collect(Collectors.toList());

这将过滤列表,仅保留员工所在的"Grade1"并返回与之对应的员工对象。这将创建grade1Employees变量。然后,我们可以使用for循环来获取他们的ID。

英文:

You don't need the map. You can do this using streams. To get Grade1, you could say:

List&lt;Employees&gt; grade1Employees = lt.stream().filter(e -&gt; e.getGrade() == &quot;Grade1&quot;).collect(Collectors.toList());

This would filter the list for where the employees are in "Grade1" and return the employee objects for them. This creates the grade1Employees variable. Then we could just use a for loop to get their IDs.

huangapple
  • 本文由 发表于 2020年8月9日 23:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63327928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定