Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 FunctionalInterface

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));
like image 519
ArunM Avatar asked Jan 22 '19 03:01

ArunM


People also ask

What is the functional interface in Java 8?

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.

How many types of functional interfaces are there in Java 8?

In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.

Why do we use functional interface in Java?

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.

Do we have functional interfaces earlier to Java 8?

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.

What is @functional interface in Java 8?

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.

What is @FunctionalInterface annotation in Java?

@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.

Can a functional interface extend another functional interface?

A functional interface can extends another interface only when it does not have any abstract method.

Can a functional interface have methods of object class?

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.


1 Answers

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);
like image 92
Ravindra Ranwala Avatar answered Oct 13 '22 00:10

Ravindra Ranwala