英文:
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<Employees> lt = new ArrayList<Employees>();
lt.add(new Employees("111", "Grade1")); //111 is the employee id
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"));
..............................
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<String, List<String>> dataMap= new HashMap<>();
dataMap.put("Grade1", List1); List contains "111" and "666"
dataMap.put("Grade2", List2); List contains "222" and "333"
dataMap.put("Grade3", List3); List contains "444" and "555"
...............................
Thank you in advance for any help you can provide
答案1
得分: 4
你可以使用Collectors.groupingBy
收集器来完成这个任务。假设你的Employees类有getGrade
和getEmployeeID
方法,你可以这样写:
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<String, List<String>> 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<Employees> grade1Employees = lt.stream().filter(e -> e.getGrade() == "Grade1").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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论