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
32
votes
6 answers

Is there a way to use Java 8 functional interfaces on Android API below 24?

I can use retrolambda to enable lambdas with Android API level <24. So this works myButton.setOnClickListener(view -> Timber.d("Lambdas work!")); This also works Runnable runLater = () -> Timber.d("Lambdas work!"); runLater.run(); But this one…
sasha199568
  • 1,143
  • 1
  • 11
  • 33
32
votes
4 answers

Implementing an interface with two abstract methods by a lambda expression

In Java 8 the lambda expression is introduced to help with the reduction of boilerplate code. If the interface has only one method it works fine. If it consists of multiple methods, then none of the methods work. How can I handle multiple methods?…
Soumya Kanti Naskar
  • 1,021
  • 3
  • 16
  • 29
28
votes
3 answers

Casting Java functional interfaces

As always I was looking through JDK 8 sources and found very interesting code: @Override default void forEachRemaining(Consumer action) { if (action instanceof IntConsumer) { forEachRemaining((IntConsumer) action); }…
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
27
votes
4 answers

Should 'Comparable' be a 'Functional interface'?

The definition of a functional interface is "A functional interface is an interface that has just one abstract method (aside from the methods of Object ), and thus represents a single function contract." According to this definition, the…
user2363727
  • 275
  • 3
  • 6
27
votes
8 answers

Why does Comparator declare equals?

The Comparator interface has its own equals() method. Any class will get equals() by default through Object class. What is the need to have equals() method inside an interface?
sagar sinha
  • 271
  • 3
  • 3
25
votes
3 answers

Java method accepting different functional interface types - possible?

First of all, sorry for the bad title, but I find it somewhat difficult to summarize my issue in one short sentence... There's some code in our software which I'm quite unhappy with. It goes like this: @FunctionalInterface public interface…
danB
  • 311
  • 2
  • 5
21
votes
4 answers

Lambda can only be used with functional interface?

I did this: public class LambdaConflict { public static void main(String args[]){ //* System.out.println(LambdaConflict.get( (str) -> "Hello World!! By ME?" )); /*/ …
Valen
  • 1,693
  • 1
  • 20
  • 17
20
votes
1 answer

Functional Interface Inheritance Quirk

I have a custom interface I've been using for some time that looks something like this: public interface Function { R call(T input); } I'd like to retrofit this interface with both Java's Function as well as Guava's Function, while…
shmosel
  • 49,289
  • 6
  • 73
  • 138
19
votes
3 answers

Method reference is ambiguous for Thread.sleep

I've come across a weird problem where a method reference to Thread::sleep is ambiguous but a method with the same signature is not. package test; public class Test { public static void main(String[] args) { foo(Test::sleep,…
Winter
  • 3,894
  • 7
  • 24
  • 56
19
votes
3 answers

Why isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?

Java 8 gave us many fun ways to use functional interfaces and with them a new annotation: @FunctionalInterface. Its job is to tell the compiler to yell at us if we fail to stick to the rules of a functional interface (only one abstract method that…
candied_orange
  • 7,036
  • 2
  • 28
  • 62
18
votes
5 answers

Do you have a list of Java 8 Functional interfaces (not the ones listed in java.util.function)?

I'm trying to see if there is any way to get a list of all the interfaces in Java 8 that are functional interfaces. I'm not talking about the list on this…
Elisabeth
  • 2,972
  • 6
  • 35
  • 39
17
votes
2 answers

Java lambda only throwing expression-style instead of statement-style

In Java (using Java 8 currently), I can write this and all will compile nice and well: Supplier asd = () -> { throw new RuntimeException(); }; Yet, I cannot write this: Supplier asd = () -> throw new RuntimeException(); // This…
Tamir Nauman
  • 279
  • 1
  • 4
17
votes
1 answer

Warning: [overloads] method m1 is potentially ambiguous with method m2

import java.util.function.*; class Test { void test(int foo, Consumer bar) { } void test(long foo, Consumer bar) { } void test(float foo, Consumer bar) { } void test(double foo, Consumer
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
16
votes
2 answers

Why is this Java method call considered ambiguous?

I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String…
15
votes
4 answers

Why to use @FunctionalInterface annotation in Java 8

If we have Interface with only one abstract method in it, it is by default Functional Interface. Can anyone please explain what additional advantage @FunctionalInterface annotation brings? I know that if we add @FunctionalAnnotation, it will not…
javaguy
  • 927
  • 2
  • 16
  • 37
1
2
3
38 39