重复密钥-误导异常信息?

时间:2018-11-22 11:46:10

标签: java java-8 java-stream

异常消息抱怨重复键,却显示值是什么原因?

List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 40));
employees.add(new Employee("John", 30));

Map<String, Integer> map = employees.stream()
    .collect(Collectors.toMap(Employee::getName, Employee::getAge));

不是显示“ John”作为重复键,而是显示“ 40”

Exception in thread "main" java.lang.IllegalStateException: Duplicate key 40
    (...)

1 个答案:

答案 0 :(得分:5)

它已在JDK 9中修复。请在此处查看。 https://bugs.openjdk.java.net/browse/JDK-8173464