Created
December 9, 2019 11:58
-
-
Save ariesmcrae/2c0ef40eeb7e96c43943883aa6da5bc4 to your computer and use it in GitHub Desktop.
Java: Find enum in list of enums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static <T extends Enum<T>> boolean isInputInTheList(T input, List<? extends Enum<?>> list) { | |
return Optional.ofNullable(list) | |
.map(Collection::parallelStream) | |
.orElseGet(Stream::empty) | |
.anyMatch(x -> input!= null && ((Enum) x).name().equals(input.name())); | |
} | |
// or for the longer version | |
private static <T extends Enum<T>> boolean isInputInTheList(T input, List<? extends Enum<?>> list) { | |
boolean isInputInList = false; | |
if (input == null) { | |
return false; | |
} | |
for (Enum<?> item : list) { | |
if (item.name().equals(input.name())) { | |
isInputInList = true; | |
break; | |
} | |
} | |
return isInputInList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment