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

Converting java.util.function.Function to Kotlin's functional interface type

I am working with two modules that are coded in Java and Kotlin respectively, hence require some bit of interoperability. In one of the cases the java module returns functional interfaces with the type java.util.function.Function, which is to be…
tirtha2shredder
  • 105
  • 1
  • 7
4
votes
1 answer

Kotlin @FunctionalInterface compiles with multiple abstract methods

When trying to compile a Java @FunctionalInterface having more than 1 non-abstract method a compilation error is raised. However, when doing the same in Kotlin, no errors or warnings are raised, i.e. the following Kotlin interface compiles…
user2340612
  • 10,053
  • 4
  • 41
  • 66
4
votes
1 answer

Java 8 Function - "wrapper" function that does something before executing given lambda?

We have the following scenario: Mid-test, some context variables need to be updated. Where exactly in the test and what exactly should happen is variable. I would like to provide a "wrapper" function, which sets up some context variables and then…
filpa
  • 3,651
  • 8
  • 52
  • 91
4
votes
1 answer

Java 8 - Comparator with nested objects

I am writing a comparator to compare two employee objects. Here i am trying to compare two employee object based on their department ,followed by their name and id respectively. The issue which i am facing here is the comparison with primitive and…
Dharmvir Tiwari
  • 886
  • 3
  • 12
  • 25
4
votes
1 answer

How to wrap a function with andThen or similar in Java functional interface

I have some repetitive code: router.post("/fleets/:fleetid/vehicles/:boxid/ping").handler(ctx -> pingBox(pingBoxTimer, oemUrl, ctx)); router.post("/fleets/:fleetid/vehicles/:boxid/wakeup").handler(ctx -> wakeUp(wakeupTimer, oemUrl,…
Orkun
  • 6,998
  • 8
  • 56
  • 103
4
votes
1 answer

How can I extend a non generic interface into a generic one?

I'm trying to extends TemporalAdjuster so that one looks like, public interface TypedTemporalAdjuster> { T adjustInto(T temporal); } When I tried to directly extends the base interface, public…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
4
votes
3 answers

Invoking .map() on an infinite stream?

According to Javadocs for SE 8 Stream.map() does the following Returns a stream consisting of the results of applying the given function to the elements of this stream. However, a book I'm reading (Learning Network Programming with Java, Richard…
4
votes
2 answers

What is the use of inheriting object class methods in functional interface eg- toString, equals

I found following code, What is use of inherited equals() and toString() method. @FunctionalInterface public interface FunInterface { // An abstract method declared in the functional interface int test(T o1, T o2); //…
Raj N
  • 249
  • 1
  • 18
4
votes
4 answers

Using functional interface as parameters to filter() function in Java 8

This is a code snippet that tries to use a functional interface inside the filter function. Function isNotPartitionFile = (path) -> { return !path.toString().contains("partition"); }; List pathsList = …
kaushalpranav
  • 1,725
  • 24
  • 39
4
votes
3 answers

Method Reference - passing Function to method with Consumer argument

I'm learning about Method References from Java 8 and I have difficulties understanding why does this work? class Holder { private String holded; public Holder(String holded) { this.holded = holded; } public String…
4
votes
2 answers

@FunctionalInterface inheritance

Say I have the following interface: @FunctionalInterface public interface First { int fun(int a); } and also @FunctionalInterface public interface Second extends First { default int fun(int a) { fun(a, a); } int fun(int a, int b); } Then…
AndresQ
  • 803
  • 5
  • 19
4
votes
2 answers

if functional interface extends another interface is it still a functional interface?

Java 8 in Action book by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft stats that: public interface Adder{ int add(int a, int b); } public interface SmartAdder extends Adder{ int add(double a, double b); } SmartAdder isn’t a functional…
codeme
  • 861
  • 2
  • 12
  • 29
4
votes
4 answers

Concept of functional interface

When I'm shooting a glance at lambda expressions, the book touches on a functional interface that has only one abstract method. My issue addresses on that quiz question /* Which of these interfaces are functional interfaces? */ public interface…
4
votes
3 answers

Meaning and usage of Predicates Java 8

I was learning the concepts of functional interfaces, lambda expression and predicates. I could write a program using examples on the internet but I am still not clear with certain constructs. This is my class Employee with 3 data members, a…
Random Coder
  • 329
  • 2
  • 5
  • 11
4
votes
3 answers

Correct approach to using MethodHandleProxies

In a Java project I'm currently working on, I'm dynamically loading classes then using the reflection API to find and execute methods of those classes that have certain annotations. The code that performs the actual execution works exclusively in…
Mac
  • 14,615
  • 9
  • 62
  • 80