public static <E extends Enum<E>> boolean validateEnum(Class<E> clazz, String s) {
return EnumSet.allOf(clazz).stream().anyMatch(e -> e.name().equals(s));
}
Following is an example invocation of the above method
boolean isValid = validateEnum(Animal.class, "DOG");
boolean isValid = validateEnum(Color.class, "Red");
Can this same functionality be implemented using a Java 8 FunctionalInterface
. I have tried creating a BiPredicate
but am getting compiler errors when I try this.
final BiPredicate<String,Class> biPre = (string1, clazz) -> EnumSet.allOf(clazz).stream().anyMatch(e -> e.name().equals(s));
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.
In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.
Objects are the base of java programming language and we can never have a function without an Object, that's why Java language provide support for using lambda expressions only with functional interfaces.
There were plenty of interfaces with a single abstract method even before Java 8. The term functional interface was introduced in Java 8. Runnable and Callable interfaces are commonly used in multithreaded applications. ActionListener interface is commonly used in Swing framework based applications when making GUIs.
A functional interface is a concept that was introduced in Java 8. An interface that has the only a single abstract method and marked with @FunctionalInterface annotation is called functional interface. The functional interface is used to support the functional programming approach, lambda expression, and method reference as well.
@FunctionalInterface Annotation @FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method. In case more than one abstract methods are present, the compiler flags an ‘Unexpected @FunctionalInterface annotation’ message. However, it is not mandatory to use this annotation.
A functional interface can extends another interface only when it does not have any abstract method.
A functional interface can have methods of object class. See in the following example. // It can contain any number of Object class methods. A functional interface can extends another interface only when it does not have any abstract method. In the following example, a functional interface is extending to a non-functional interface.
Here's a one way of doing it,
final BiPredicate<String, ? super Enum<?>> biPre = (string1, enumType) -> EnumSet
.allOf(enumType.getDeclaringClass()).stream().anyMatch(e -> e.name().equals(string1));
And here's the client code,
boolean test = biPre.test("DOG", Animal.CAT);
However passing an enum constant instead of a class literal seems a bit awkward here.
If you really need to use the type token here's what you should do,
final BiPredicate<String, Class<? extends Enum<?>>> biPre = (string1, clazz) -> Arrays
.stream(clazz.getEnumConstants()).anyMatch(e -> e.name().equals(string1));
The client now looks like this,
boolean test = biPre.test("DOG", Animal.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With