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

Java 8 lambdas execution

How can I do something like this in Java 8? boolean x = ((boolean p)->{return p;}).apply(true); Right now I get the following error: The target type of this expression must be a functional interface
Abdul Rahman
  • 1,294
  • 22
  • 41
15
votes
3 answers

Why can't @FunctionalInterface be applied to a SAM abstract base class

I'm just starting to learn Camel and the first thing I see is context.addRoutes(new RouteBuilder() { public void configure() { from("file:data/inbox?noop=true").to("file:data/outbox"); } }); which I (reasonably…
Sarah Phillips
  • 923
  • 11
  • 30
14
votes
1 answer

Can you call the parent interface's default method from an interface that subclasses that interface?

In java 8 I have something like this: package test; public class SimpleFuncInterfaceTest { public static void carryOutWork(AFunctionalInterface sfi){ sfi.doWork(); } public static void main(String[] args) { …
ggb667
  • 1,881
  • 2
  • 20
  • 44
13
votes
2 answers

Java Lambda to comparator conversion - intermediate representation

I'm trying to make sense of how Comparator.comparing function works. I created my own comparing method to understand it. private static > Comparator comparing(Function f) { BiFunction bfun = (T a, T…
patentfox
  • 1,436
  • 2
  • 13
  • 28
13
votes
1 answer

Passing an instance of Comparable to a method that expects a Comparator

The Stream class in Java 8 defines a max method that requires a Comparator argument. Here is the method signature: Optional max(Comparator comparator) Comparator is a functional interface that has an abstract compare method with this…
Bryan
  • 133
  • 1
  • 6
12
votes
3 answers

Static reference ( with :: ) to a method returning an interface

I have several predifined static "processors" implementing the same method, for example: default double process(double num){ Sample : public class Test { public static void main(String[] args) { test(Test::processor1, 1d); …
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
11
votes
1 answer

Function interface in Kotlin 1.4

This feature will be coming Kotlin 1.4. Here is an excerpt from KotlinConf'19. fun interface Action { fun run() } fun runAction(a: Action) = a.run() runAction{ println("Hello") } It looks nice, but I still don't know what it does. What is…
hzqelf
  • 857
  • 7
  • 17
11
votes
4 answers

Unexpected Java Functional Interface conversion

I have the following piece of code, that uses java Functional Interfaces, that compiles, but it's not clear why does it compile: public class App { public static void main(String[] args) throws Exception { final RecordIterator it = new…
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
11
votes
6 answers

Using Java Predicate and Lambda

Why does the below code return Predicate and not boolean? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean. So in my lambda should my…
Taobitz
  • 546
  • 3
  • 10
  • 22
11
votes
3 answers

Java method can't be applied with Lambda expression

I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine. Runner runner = new Runner(); runner.run(() -> System.out.println("Print…
Smith Lo
  • 305
  • 3
  • 12
11
votes
1 answer

reference to method is ambiguous when migrating from java8 to java9

I'm migrating a project from JAVA 8 to JAVA 9 and I'm having some trouble getting the code to work. All work in JAVA 8 but in 9 I'm having the following errors: Error java: reference to ok is ambiguous both method…
11
votes
3 answers

How to suitably compose Predicate and Function in a Java function?

The purpose is to create a new Predicate usable in a stream filter : myCollectionOfElement .stream() .filter( …
fdelsert
  • 768
  • 1
  • 10
  • 22
11
votes
4 answers

Please Explain Java 8 Method Reference to instance Method using class name

public interface MyFunc { boolean func(T v1, T v2); } public class HighTemp { private int hTemp; HighTemp(){ } public HighTemp(int ht) { this.hTemp = ht; } boolean sameTemp(HighTemp ht2){ return…
10
votes
2 answers

Method reference does not always seem to capture instance

I know there are many questions on the subject even a very recent one but I still can't work my head around one thing. Consider the following functional interface: @FunctionalInterface interface PersonInterface { String getName(); } And this…
Bentaye
  • 9,403
  • 5
  • 32
  • 45
10
votes
6 answers

How to ensure at Java 8 compile time that a method signature "implements" a functional interface

Is there in Java 8 any analogue for implements keyword for methods? Let's say I have a functional interface: @FunctionalInterface interface LongHasher { int hash(long x); } And a library of 3 static methods "implementing" this functional…
Yahor
  • 639
  • 8
  • 16
1 2
3
38 39