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

Method call on a Lambda object returns value

the expression this.apply(stringValue) returns true on a lambda expression j8Test.PerformChange$$Lambda$2/284720968@2f4d3709 Why does it return true and not false. And why doest it return actually anything at all? (I am not talking about all other…
Anna Klein
  • 1,906
  • 4
  • 27
  • 56
3
votes
2 answers

lambda expression for Consumer Interface with return type

The thing I don't quite understand is why java compiler allows a lambda expression such as s -> s.isEmpty() inside a consumer interface. I have tried a lambda expression like s -> s.isEmpty() for a Consumer interface and it works without issue. Some…
user11944316
3
votes
2 answers

Target type casting for method arguments , Lambda

Consider the following two functional interfaces ( java.lang.Runnable and java.util.concurrent.Callable): public interface Runnable { void run(); } public interface Callable { V call(); } Suppose that you have overloaded the method…
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
3
votes
3 answers

How/Why can we say that the functional interfaces in java 8 are marker interfaces

In a recent interview I was asked the question that "how can we say that in java8 the functional interfaces are similar to marker interfaces". I was not able to answer this question. But I think marker does not even have any methods while functional…
3
votes
3 answers

Converting array iteration to lambda function using Java8

I am trying to convert to Lambda function So far I am able to convert the above code to lambda function like as shown below Stream.of(acceptedDetails, rejectedDetails) .filter(list -> !isNull(list) && list.length > 0) .forEach(new Consumer()…
Alex Man
  • 4,746
  • 17
  • 93
  • 178
3
votes
2 answers

Functional Interface Implementations and use cases

I was just trying to write a functional interface to understand the different use cases. Looking at the below code which I have written, I understand that I can have different implementations using lambda expressions. Apart from this can anyone…
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60
3
votes
2 answers

How the functional interface working in java 8

Here is an example I ran across while studying the functional interface concept. interface Sayable{ void say(); } public class MethodReference { public static void saySomething(){ System.out.println("Hello, this is static…
mallikarjun
  • 1,862
  • 5
  • 23
  • 47
3
votes
2 answers

Custom functional interface as method argument

I have the code below that does produce expected result. Could somebody explain what's happening behind the curtains? I don't understand how does the compiler/JVM knows that it's needed to invoke store(String str) on the Storer object or how does it…
Anton
  • 57
  • 8
3
votes
2 answers

How to apply in Function argument from List (not List)

I have a method which returns company as key and list of employeer as values Map> getUserPerCompany(final Function converter). The method accepts the converter parameter which in the tests returns the String (name +…
3
votes
1 answer

Functional interface + lambda expression for sum of integers in an array

I'm tackling an exercise that asks to create a functional interface whose method takes as input an integer k and an array of integers and returns an integer. Then, I should assign to an instance of the interface a lambda expression that returns the…
3
votes
8 answers

How to provide implementations for enum values in Java?

I have enum class with values which are suppose to grow by time and I want users who add new enum values also provide the impementation somewhere. But I am not sure how to force them to provide impementation as impementation will be in some other…
user1298426
  • 3,467
  • 15
  • 50
  • 96
3
votes
1 answer

Function Interfaces calling Spring beans

I want to map specific types to trigger Spring methods, I save Map of Function Interfaces by key, the functions will call Spring services method, but I have an issue that it must be static, e.g.: private Map, Function
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
3
votes
3 answers

BinaryOpertor for List to add the lists

In previous question I asked previously Which FunctionalInterface should I use? Now I was trying to add to List and not just two Integers a and b, such that each index adds to the same index of another list. I had previously …
Mani
  • 1,068
  • 3
  • 13
  • 27
3
votes
1 answer

Kotlin shorthand for implementing a functional interface with annotations

In Kotlin, is there a way to define an annotated class implementing a functional interface that is shorter than the following: @Foo class Bar : Runnable { override fun run() = ... } I'm hoping to find something like the following made-up…
A.L.
  • 760
  • 5
  • 13
3
votes
2 answers

How can I use reference method in a UnaryOperator java 8

Currently, I have a UnaryOperator like this UnaryOperator defaultParser = obj -> obj; I don't know if I can use a method reference in these kinds of operation. Example: UnaryOperator defaultParser = String::toString; But with the…