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
189
votes
11 answers

What are functional interfaces used for in Java 8?

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions. Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make…
Madhusudan
  • 4,637
  • 12
  • 55
  • 86
135
votes
7 answers

When and why would you use Java's Supplier and Consumer interfaces?

As a non-Java programmer learning Java, I am reading about Supplier and Consumer interfaces at the moment. And I can't wrap my head around their usage and meaning. When and why you would use these interfaces? Can someone give me a simple layperson…
james emanon
  • 11,185
  • 11
  • 56
  • 97
109
votes
8 answers

Java 8 Supplier with arguments in the constructor

Why do suppliers only support no-arg constructors? If the default constructor is present, I can do this: create(Foo::new) But if the only constructor takes a String, I have to do this: create(() -> new Foo("hello"))
cahen
  • 15,807
  • 13
  • 47
  • 78
78
votes
9 answers

Precise definition of "functional interface" in Java 8

Recently I started exploring Java 8 and I can't quite understand the concept of "functional interface" that is essential to Java's implementation of lambda expressions. There is a pretty comprehensive guide to lambda functions in Java, but I got…
Anton Cherkashyn
  • 5,719
  • 6
  • 43
  • 80
61
votes
2 answers

Thread.sleep inside infinite while loop in lambda doesn't require 'catch (InterruptedException)' - why not?

My question is about InterruptedException, which is thrown from the Thread.sleep method. While working with ExecutorService I noticed some weird behaviour that I don't understand; here is what I mean: ExecutorService executor =…
53
votes
9 answers

When we should use Supplier in Java 8?

What difference between this code? Supplier s1 = LocalDate::now; LocalDate s2 = LocalDate.now(); System.out.println(s1.get()); //2016-10-25 System.out.println(s2); //2016-10-25 I start learning functional interfaces in Java 8 and don't…
badCoder
  • 730
  • 1
  • 5
  • 13
53
votes
1 answer

Java interface like Predicate, but without an argument

I'm looking for a preexisting functional interface like Predicate, but one whose test method takes no arguments.
Greg Valvo
  • 1,069
  • 1
  • 9
  • 13
51
votes
1 answer

Use method reference with parameter

I just started learning Java streams and faced a problem. Please take a look at a the following example. This is part of a Node class: private Map nodes; public Optional child(String name) { return…
48
votes
6 answers

Is it possible to declare that a Supplier needs to throw an Exception?

So I am trying to refactor the following code: /** * Returns the duration from the config file. * * @return The duration. */ private Duration durationFromConfig() { try { return durationFromConfigInner(); } catch (IOException…
skiwi
  • 66,971
  • 31
  • 131
  • 216
47
votes
5 answers

Why does a lambda change overloads when it throws a runtime exception?

Bear with me, the introduction is a bit long-winded but this is an interesting puzzle. I have this code: public class Testcase { public static void main(String[] args){ EventQueue queue = new EventQueue(); queue.add(() ->…
Gili
  • 86,244
  • 97
  • 390
  • 689
46
votes
7 answers

Why do I need a functional Interface to work with lambdas?

I think this question is already somewhere out there, but I wasn't able to find it. I don't understand, why it's necessary to have a functional interface to work with lambdas. Consider the following example: public class Test { public static…
codepleb
  • 10,086
  • 14
  • 69
  • 111
41
votes
2 answers

How can Comparator be a Functional Interface when it has two abstract methods?

In Java 8, the @FunctionalInterface annotation is introduced to denote any interface that has exactly one abstract method as a functional interface. One of the reason for its introduction is to indicate the user (programmer), that lambda expression…
Arun
  • 1,176
  • 5
  • 20
  • 48
41
votes
4 answers

Why Functional Interfaces in Java 8 have one Abstract Method?

As we know in Java 8, the concept of functional interfaces are introduced. A Functional Interface has one abstract method and several default or static methods are possible. But why should a Functional interface have only one abstract method? If…
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
36
votes
3 answers

FunctionalInterface Comparator has 2 abstract methods

Learning Java 8 Lambdas and just wondering how the compiler knows which method in Comparator to use for the lambda expression? It doesn't seem to be a SAM interface? It has 2 abstract methods: @FunctionalInterface public interface Comparator { …
DarVar
  • 16,882
  • 29
  • 97
  • 146
33
votes
4 answers

Callable vs Supplier interface in java

The Callable and Supplier functional interfaces in java.util.concurrent and java.util.function packages respectively have the following signature- public interface Callable { V call() throws Exception; } public interface Supplier { T…
arunkjn
  • 5,631
  • 5
  • 21
  • 31
1
2 3
38 39