Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/lang3/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumC
* @since 3.13.0
*/
public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction) {
return stream(enumClass).collect(Collectors.toMap(keyFunction::apply, Function.identity()));
return stream(enumClass).collect(Collectors.toMap(keyFunction::apply, Function.identity(), (existing, replacement) -> existing));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/commons/lang3/EnumUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ void testStream() {
assertEquals(0, EnumUtils.stream(null).count());
}

@Test
void testGetEnumMapOnDuplicateKey() {
assertEquals(1,EnumUtils.getEnumMap(DuplicateKeyEnum.class, e -> e.key).size());
}
}

enum Month {
Expand Down Expand Up @@ -667,3 +671,11 @@ public int getValue() {
return value;
}
}

enum DuplicateKeyEnum {
A("x"), B("x");

final String key;

DuplicateKeyEnum(String key) { this.key = key; }
}