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
5
votes
1 answer

No target method found in functional interface

I am trying to learn more about the Java 8 FunctionalInterface annotation. I wrote the following code as an experiment, but it does not compile: @FunctionalInterface public interface HasToString { String toString(); } No target method…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
5
votes
1 answer

Create generic Java method wrapper for pre and post processing

I'm basically trying to create a static method that will serve as a wrapper for any method I pass and will execute something before and after the actual execution of the method itself. I'd prefer to do it using Java 8 new coding style. So far I have…
Jesus
  • 318
  • 1
  • 4
  • 17
5
votes
2 answers

Groovy closures and overloaded methods with functional parameters

I get Ambiguous error when I try to use a code which contains overloaded methods with functional arguments. I wrote a small snippet which shows an ambiguous behavior: import java.util.function.BiConsumer import java.util.function.Consumer class…
5
votes
4 answers

Why different predicate interfaces n JAVA 8?

In Java 8 different predicate interfaces (ex. DoublePredicate, LongPredicate, IntPredicate, etc.) are provided. Now if you are going to implement the interface and write your own code in it, what is the advantage of having different predicate…
Milind Vinkar
  • 159
  • 1
  • 9
5
votes
1 answer

Convert Consumer into Runnable inside Stream.map()

I am trying to convert a Consumer to a Runnable. The following code does not generate any compiler errors in Eclipse IDE. Consumer consumer; Runnable runnable; Object value; ... runnable = () -> consumer.accept(value); The following code…
Nathan
  • 8,093
  • 8
  • 50
  • 76
5
votes
1 answer

How can I search a java code base for interfaces that have a single method?

A project I work on has recently switched from Java 7 to Java 8. I'd like to be able to find interfaces that have a single abstract method as candidates for introducing functional interfaces into our code base. (Annotating existing interfaces as…
smithkm
  • 231
  • 1
  • 9
5
votes
1 answer

Automatic constructor matching in default method

I have a PersonFactory interface as follows: @FunctionalInterface public interface PersonFactory

{ P create(String firstname, String lastname); // Return a person with no args default P create() { // Is there a…

5
votes
1 answer

Java 8 Lambda with Function as argument

Interesting compilation error in Lambda Java 8 (Oracle JDK) java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) I have a method call: new…
user1189332
  • 1,773
  • 4
  • 26
  • 46
5
votes
2 answers

java 8 function invocation

I have been using Java 8 from last couple of months and trying to get my head around lambdas. I have a quite bit understanding about concert. But struggling with custom functional interface execution as lambda call. If I create java Bifuctional…
Maddy
  • 73
  • 1
  • 5
4
votes
1 answer

How to use lambdas together with functional interfaces and directly assign lambda to var in kotlin

I have this code: package org.medianik.kotlindoc fun main(){ val creator:Creatable = toCreatable(::Created)// Ok. Compiles, and works fine val creator1:Creatable = ::Created // Compilation error val creator2:Creatable = ::Created as…
4
votes
3 answers

BiSupplier in Java8

I have seen BiConsumer, BiPredicate, BiFunction but not BiSupplier or similar. I tried the below code but got an exception saying: "Multiple non-overriding abstract methods found in BiSupplier". @FunctionalInterface public interface BiSupplier
Alex Man
  • 4,746
  • 17
  • 93
  • 178
4
votes
1 answer

How to configure the bindings of a function in Spring Cloud Stream to bind its input to a web endpoint and its output to a Kafka topic

I have a regular java Function; which I am trying to bind: Its input to a web endpoint Its output to a kafka topic. When I use my function in the context of the web, it always returns the resulting value of the Function back to the web client…
Marco R.
  • 2,667
  • 15
  • 33
4
votes
2 answers

Benefits of @FunctionalInterface in staged builders

Introduction While searching the web I stumbled upon a blog post by Benoit Tellier, Next level Java 8 staged builders, where he shares his variant of the staged builder pattern for certain use cases. I noticed that stages are annotated with…
Rolf W.
  • 1,379
  • 1
  • 15
  • 25
4
votes
3 answers

Java 8: How BiFunction works while using andThen() and apply() methods

Currently BiFunction interface has two methods andThen() and apply(). I found different examples in web, but the below example is something I can't figure out - 'How the chaining of these methods works' Example 1: BiFunction
MKod
  • 803
  • 5
  • 20
  • 33
4
votes
3 answers

Using BiFunction in place of a Comparator does not work

While initializing collections like TreeMap, TreeSet, etc. We can add our custom comparator. The code looks something like: Map map1 = new TreeMap<>(new Comparator() { public int compare(Integer x, Integer y) { …
YetAnotherBot
  • 1,937
  • 2
  • 25
  • 32