【问题标题】:In Java 8 if map is having duplicate values getting duplicate values exceiption在 Java 8 中,如果 map 具有重复值,则会出现重复值异常
【发布时间】:2018-05-22 11:44:10
【问题描述】:

代码正在生成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)

【问题讨论】:

  • 这个问题的格式不正确。我们需要对问题的描述、最少的代码、好的标题和好的标签。请阅读stackoverflow.com/help/how-to-ask 以改进您的问题。
  • 您确定您所提供的确切代码会出现此异常吗?
  • 确定我以后会发布正确的格式@Opsse
  • 抱歉,@mks,它已修复。感谢您纠正我。
  • 所以你得到的正是what the documentation specifies:“如果映射的键包含重复项……,则在执行收集操作时会抛出IllegalStateException。如果映射的键可能有重复,请改用toMap(Function, Function, BinaryOperator)”...

标签: java filter java-stream


【解决方案1】:

第一个解决方案:你需要知道职位的固定工资 - 没有总和。

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

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));

结果如下:

qe -> 154222.98

开发-> 168231.98

经理 -> 100666.99

铅 -> 272027.97000000003

Java 文档:Collector

【讨论】:

  • @RazzeshCh 欢迎您。如果答案让您满意,您可以将其标记为“解决方案”吗?
  • 由于声誉低,我无法对您的答案进行投票,请告诉我如何以其他方式将您的解决方案标记为答案,我是新手。
猜你喜欢
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2017-09-16
  • 2020-11-15
  • 1970-01-01
  • 2018-09-08
相关资源
最近更新 更多