在Java 8中,如果map具有重复值,则会出现重复值异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Java 8中,如果map具有重复值,则会出现重复值异常相关的知识,希望对你有一定的参考价值。

如果列表中有重复值,并且我们尝试使用java流从列表中提取映射,则代码生成IllegalStateException: Duplicate key。如果我们在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("

 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)
答案

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

您可以将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));

这将打印输出:

铅 - > 90675.99

注意:

  • Employee :: getJob与你的emp-> 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));

结果如下:

那 - > 154222.98

dev - > 168231.98

mgr - > 100666.99

铅 - > 272027.97000000003

Java Doc:Collector

以上是关于在Java 8中,如果map具有重复值,则会出现重复值异常的主要内容,如果未能解决你的问题,请参考以下文章

Java找到具有特定值的所有键[重复]

java中hashset和hashmap有啥特点?

如果只有一个值,则返回一个值的 Java 8 收集器 [重复]

Java 8 Streams - 分组为单个值[重复]

Java中的Map允许有重复元素吗

java 请求参数键值对重复问题