Questions tagged [functional-interface]

A functional interface in the Java language refers to an interface with a single abstract method. @FunctionalInterface is an annotation which requires a particular interface declaration to conform to this specification. The target type of a lambda expression or method reference must be a functional interface. Functional interfaces are part of the Java 8 feature set.

A functional interface in the language refers to an interface with a single abstract method. @FunctionalInterface is an annotation which requires the interface to conform to this specification. The target type of a lambda expression or method reference must be a functional interface. Functional interfaces are part of the feature set.

The following is a simple functional interface:

@FunctionalInterface
interface StringFunction<T> {
    String applyToString(T obj);
}

The single abstract method declaration allows it to be the target of a lambda expression:

StringFunction<Integer> toHexStringFn =
    (Integer n) -> Integer.toHexString(n);

If the interface has more than one abstract method, it is not longer a functional interface and can no longer be the target of a lambda.

Functional interfaces should be annotated with the @FunctionalInterface annotation, which requires the interface to conform to the functional interface specification. An interface annotated with @FunctionalInterface will cause a compilation error if the interface has e.g. more than one abstract method. (This is similar to the behavior of the @Override annotation, which will cause a compilation error if a method override is incorrect.)

See also:

571 questions
9
votes
3 answers

Create custom Predicate with Set and String as parameter

I have a String as "ishant" and a Set as ["Ishant", "Gaurav", "sdnj"] . I need to write the Predicate for this. I have tried as below code, but it is not working Predicate,String> checkIfCurrencyPresent = (currencyList,currency)…
Ishant Gaurav
  • 1,183
  • 2
  • 13
  • 32
9
votes
2 answers

How does java differentiate Callable and Runnable in a Lambda?

I got this little code to test out Callable. However, I find it pretty confusing how the Compiler could know if the Lambda is for the Interface Callable or Runnable since both don't have any parameter in their function. IntelliJ, however, shows…
curiouscupcake
  • 1,157
  • 13
  • 28
9
votes
3 answers

Predicate from Function Reference (of boolean type)

I need to compose a stream operation with a predicate based on a boolean function. Found a workaround via rethrowing a method's argument as a predicate, as shown: public Predicate pred(final Predicate aLambda) { return…
Tomas F.
  • 610
  • 1
  • 6
  • 13
9
votes
4 answers

Are there dangers in making an existing Java interface functional?

As a rule, in the context of a large project, is it considered safe to take make an existing, ubiquitously used interface into a functional interface? E.g., given an existing interface and class: public interface Interface { public double…
Frank Harris
  • 587
  • 2
  • 16
9
votes
2 answers

Java 8 multiple mapping

Is it possible perform multiple mapping on collection? Following code compilation error: ... in Stream cannot be applied to java.util.function.Function,capture> private static List multipleMapping(final Collection collection,…
Artur
  • 217
  • 1
  • 5
  • 12
9
votes
2 answers

Is there a way to turn an existing interface into a functional interface?

I am using an interface that looks something along the lines of this: public interface ObjectListener { public void objectAdded(Object o); public void objectRemoved(Object o); } And I am currently using an anonymous class to implement the…
Jazzer
  • 2,993
  • 1
  • 19
  • 24
9
votes
3 answers

Java 8 streams, why does this compile part 2... Or what is a method reference, really?

OK, the first question in this "series" was this one. Now, here is another case: Arrays.asList("hello", "world").stream().forEach(System.out::println); This compiles, and works... OK, in the last question, static methods from a class were used. But…
fge
  • 119,121
  • 33
  • 254
  • 329
8
votes
1 answer

Looking for "consumer that returns value" abstraction in Java

Is there a built-in or robust third-party abstraction for consumer that returns value in Java 8+? P.S. For deferred execution it may return Future as well. Update. Function interface has a perfect syntactic match, but there is a consideration around…
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
8
votes
2 answers

Code compiles in Eclipse but not javac: curried lambdas with functional subinterface. Which is correct?

I developed some code in Eclipse, tested it successfully, pushed it to our Jenkins CI server, and got an email that Maven was choking with a Java compile error. I subsequently isolated the problem and created the following minimal example showing…
Aaron Rotenberg
  • 972
  • 7
  • 22
8
votes
2 answers

Why can't we overload a abstract method in a functional interface? (Java)

So I am familiar with functional interfaces in java, and their use with lambda expressions. A functional interface can only contain one abstract method. When using this lonely method from a lambda expression, you do not need to specify its name -…
HomeworkHopper
  • 300
  • 1
  • 12
8
votes
2 answers

How to implement BiFunctional function that corresponds to Enum in Java?

I have Java enum: public enum ConflictResolutionStrategy { softResolve, hardResolve, } I want to call it like ConflictResolutionStrategy.hardResolve.apply(case1, case2). Both case1 and case2 objects of the same type. apply in my case should…
Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
8
votes
2 answers

Predicate in Java11 filters all elements

I am moving to use Java11. Learning a new method Predicate.not, I found my current code to find only cat family as: List animals = List.of("cat", "leopard", "dog", "lion", "horse"); Predicate cats = a -> !a.equals("dog") &&…
Mani
  • 1,068
  • 3
  • 13
  • 27
8
votes
2 answers

Replacing switch by BinaryOperator

I'm trying to replace the common switch for arithmetical operations by BinaryOperator functional interface. The base method is: private static int computeOne(int res, String operand, String operation) { int number =…
8
votes
3 answers

Purpose of Functional Interfaces in Java8

I've come across many questions in regards of Java8 in-built Functional Interfaces, including this, this, this and this. But all ask about "why only one method?" or "why do I get a compilation error if I do X with my functional interface" and alike.…
Andrejs
  • 10,803
  • 4
  • 43
  • 48
8
votes
1 answer

What is the purpose of lower bounded wildcard in Function.class?

In Function.class from Java8, we have: default Function compose(Function before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } Compose accepts: Function
Noozen
  • 379
  • 3
  • 13