Last active
October 14, 2021 08:31
-
-
Save nakaly/19614523b1ff805bf01994f59ddcc988 to your computer and use it in GitHub Desktop.
enum
This file contains 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
public class Sandbox { | |
public static void main (String... args) { | |
System.out.println(getEnums(Hoge.class, "A B")); | |
} | |
public static <T extends Enum<T>> List<T> getEnums(Class<T> enumClass, String line) { | |
return Stream.of(line.split(" ")).map(item -> getEnum(enumClass, item).get()).collect(Collectors.toList()); | |
} | |
public static <T extends Enum<T>> Optional<T> getEnum(Class<T> enumClass, String code) { | |
return EnumSet.allOf(enumClass).stream().filter(e -> e.name().equals(code)).findFirst(); | |
} | |
enum Hoge { | |
A, | |
B, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment