在Java 8中,如果map具有重复值,则会获得重复值

时间:2018-05-22 11:44:10

标签: java filter java-stream

代码正在生成IllegalStateException: Duplicate key,如果有的话 是列表中的重复值,当我们尝试提取地图时 从使用java流的列表。如果我们在collect中定义合并函数 方法,然后问题得到解决。

List<Employee> empList = new ArrayList<Employee>();
Employee e1 = new Employee(1,"aaaa","mgr",100666.99);
Employee e2 = new Employee(2,"bbbb","lead",90675.99);
Employee e3 = new Employee(3,"cccc","dev",77555.99);
Employee e4 = new Employee(4,"dddd","qe",63546.99);
Employee e5 = new Employee(5,"eeee","lead",90675.99);
Employee e6 = new Employee(6,"ffff","lead",90675.99);
Employee e7 = new Employee(7,"gggg","dev",90675.99);
Employee e8 = new Employee(8,"hhhh","qe",90675.99);
empList.add(e1);
empList.add(e2);
empList.add(e3);
empList.add(e4);
empList.add(e5);
empList.add(e6);
empList.add(e7);
empList.add(e8);

List<Department> deptList = new ArrayList<Department>();
Department d1 = new Department(1, "IT", 10);
Department d2 = new Department(2, "Sales", 20);
Department d3 = new Department(3, "HR", 30);
Department d4 = new Department(4, "Support", 40);
deptList.add(d1);
deptList.add(d2);
deptList.add(d3);
deptList.add(d4);

System.out.println("\n\n List of Leads Names :");

List<String> empNames = empList.stream().filter(emp -> "lead".equals(emp.job)).map(Employee::getName).collect(Collectors.toList());
empNames.forEach(System.out::println);

Map<String,Double> jobSalMap = empList.stream().filter(emp -> "lead".equals(emp.job)).collect(Collectors.toMap(emp->emp.job,emp->emp.sal));

jobSalMap.forEach(new BiConsumer<String,Double>(){

    @Override
    public void accept(String arg0, Double arg1) {
        System.out.println(arg0 + " " + arg1);

    }

});

double sumOfLeadSal = empList.stream().filter(emp -> "lead".equals(emp.job)).mapToDouble(Employee::getSal).sum();

System.out.println(sumOfLeadSal);

输出:

List of Leads Names :
bbbb
eeee
ffff
    Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90675.99
    at java.util.stream.Collectors.lambda$throwingMerger$0(Unknown Source)
    at java.util.HashMap.merge(Unknown Source)
    at java.util.stream.Collectors.lambda$toMap$58(Unknown Source)
    at java.util.stream.ReduceOps$3ReducingSink.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(Unknown Source)
    at com.zzar.practice.TestEmployee.main(TestEmployee.java:59)

1 个答案:

答案 0 :(得分:2)

第一个解决方案:你需要通过职称知道不变薪水 - 没有总和。

您可以将toMap函数与merge运算符一起使用:

Map<String, Double> mapByJob = empList.stream()
                .collect(Collectors.toMap(Employee::getJob, Employee::getSal, (sal1, sal2) -> sal1));

mapByJob .forEach((job, sal) -> System.out.println(job + " -> " + sal));

那将打印输出:

  

铅 - &gt; 90675.99

注意:

  • Employee :: getJob与您的emp-&gt; emp.job
  • “等效”
  • 但是自从 薪水不是一成不变的,我不会用这个解决方案(见第二篇) 溶液)。

第二个解决方案:您需要通过职位名称了解工资 - 总和

通过按工作头衔汇总工资,它变得非常方便:

Map<String, Double> mapByJob = empList.stream()
                .collect(Collectors.toMap(Employee::getJob, Employee::getSal, (sal1, sal2) -> sal1+sal2));

mapByJob .forEach((job, sal) -> System.out.println(job + " -> " + sal));

结果如下:

  

qe - &gt; 154222.98

     

dev - &gt; 168231.98

     

mgr - &gt; 100666.99

     

铅 - &gt; 272027.97000000003

Java Doc:Collector