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
8
votes
2 answers

Compiler not inferring System.out::println functional interface

I have an overloaded method that takes two different functional interfaces as parameters (Runnble and Supplier). System.out.println is clearly only compatible with Runnable, because it is a void method. Yet the compiler still claims that the call is…
Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37
8
votes
1 answer

Equality of instance of functional interface in java

I am not sure how I can be sure about equality/immutability of functional interface. I guess there might be no way to assure equality when I use this syntactic sugar in java 8, please let me know any hint if you have any. I made a short code snippet…
7
votes
1 answer

How to understand Kotlin Functional Interface with companion object inside?

I would like to get some help to understand a Kotlin code snippet about functional interface used in Http4k org.http4k.core package typealias HttpHandler = (Request) -> Response fun interface Filter : (HttpHandler) -> HttpHandler { companion…
danny
  • 3,046
  • 3
  • 23
  • 28
7
votes
2 answers

Multiline lambda

I have this code, which works: new JdbcTemplate(new SingleConnectionDataSource(c, true)) .query("select id, name from PLAYERS", (rs, rowNum) -> new Player(rs.getString("id"), rs.getString("name")) // oneline ); However…
BobJI
  • 163
  • 1
  • 9
7
votes
1 answer

Why Functional interface initialize different when use lambda in factory-method and method reference (singleton / prototype)?

I have two factory-methods which produce "consumers" use different approaches lambda and method references: @SuppressWarnings("Convert2MethodRef") public Consumer lambdaPrintStringConsumer(){ return x -> System.out.println(x); } public…
kozmo
  • 4,024
  • 3
  • 30
  • 48
7
votes
1 answer

Function which hold implementation of Runnable

I have this code: new Thread(() -> { //do things }).start(); new Thread(() -> { //do same things }).start(); I know I can declare a function which hold lambda: Function add = x -> x + 1; I want to make this function…
KunLun
  • 3,109
  • 3
  • 18
  • 65
7
votes
2 answers

How to pass Consumer to the method

so I do have code like this: public ConsumerTestClass(Consumer consumer) { } public static void printString(String text) { System.out.println(text); } And from the method of other class, I would like to create object of…
Suule
  • 2,197
  • 4
  • 16
  • 42
7
votes
2 answers

Stream.reduce(Float,BinaryOperator) BinaryOperator refers which functional interface method?

For Example, List productsList = new ArrayList(); productsList.add(new Product(1,"HP Laptop",25000f)); productsList.add(new Product(2,"Dell Laptop",30000f)); productsList.add(new Product(3,"Lenevo Laptop",28000f)); …
Rence Abishek
  • 373
  • 1
  • 4
  • 16
7
votes
3 answers

Which FunctionalInterface should I use?

I was learning to write some lambda representation as FunctionalInterface. So, to add two integers I used: BiFunction biFunction = (a, b) -> a + b; System.out.println(biFunction.apply(10, 60)); Gives me the output 70.…
Mani
  • 1,068
  • 3
  • 13
  • 27
7
votes
3 answers

Is there a way to print a functional interface?

Suppose I have an interface: public interface Function { double function (double input); } Now, suppose I have created an instance of this interface somewhere in my main class, Function f = (x) -> x; How can I go about printing this function,…
MazeOfEncryption
  • 365
  • 3
  • 13
7
votes
1 answer

Definition of Functional Interface in Java 8

The definition of a functional Interface in Java 8 says: A functional interface is defined as any interface that has exactly one explicitly declared abstract method. (The qualification is necessary because an interface may have non-abstract…
Deepak Kumar
  • 843
  • 1
  • 7
  • 19
7
votes
2 answers

Why doesn't Java 8's ToIntFunction extend Function

If I wrote the ToIntFunction interface, i'd want to encode in the interface the fact that it's just a function that returns a primitive int, like this: @FunctionalInterface public interface ToIntFunction extends Function { int…
mkadunc
  • 696
  • 6
  • 12
7
votes
5 answers

Java 8 - Functional Interface vs Abstract class

I was exploring features of Java 8 and came across "Functional Interface". As per my understanding, these interfaces can have some default implemented methods as : @FunctionalInterface public interface ComplexFunctionalInterface extends…
codingenious
  • 8,385
  • 12
  • 60
  • 90
6
votes
6 answers

How to implement a functional interface as lambda in Kotlin?

I want to implement a functional kotlin interface (interface with a single abstract method) as a kotlin lambda. How to do that? Kotlin interface @FunctionalInterface interface Foo{ fun bar(input: String): String } Kotlin implementation . fun…
Chriss
  • 5,157
  • 7
  • 41
  • 75
6
votes
2 answers

Why does functional interface with void return-type method accept any return-type method?

We have this code: public class Test { public static Object foo() { System.out.println("Foo"); return new Object(); } public static void main(String[] args) { J j = Test::foo; j.m(); } } interface J…