Lambda set to map duplicate key error solution

  If the same key exists in this method, an error will be reported

Map<String,JKStackRedEx> result = v.stream().collect(Collectors.toMap(a->a.getManuId(),a->a));

The following method can be used to avoid error reporting. The parameter returned by return can specify which value to use to override value1 or Value2

Map<String,JKStackRedEx> result = v.stream().collect(Collectors.toMap(a->a.getManuId(),a->a, (value1, value2) -> {
    return value2;
}));

You can also use multiple attributes to splice keys

Map<String,JKStackRedEx> result = v.stream().collect(Collectors.toMap(a->a.getManuId().concat(a.getStackingNum()),a->a, (value1, value2) -> {
                    return value2;
                }));

 

Read More: