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

java 8 - store method in HashMap and get return value from method in map

I want to store methods pointer in map to execute them based on a string value. Аrom what I found, I can use Map to do it, but the problem is I want to get the return value from the method. Say I have something like this: private…
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
4
votes
2 answers

Java 8 Generic java.util.Function

I am learning about FunctionalInterface which is present in Java 8. After doing some basic Functional examples, I tried to do the same with GenericType parameters. public class Main { public enum LocType { Area, Country } …
The Coder
  • 2,562
  • 5
  • 33
  • 62
4
votes
2 answers

Behavior of Functional Interface and Method Reference

What happens when the reference of a method which belongs to a variable is destroyed? public class Hey{ public double bar; public Hey(){ bar = 2.0d; } public double square(double num){ return Math.pow(num , bar); …
Michael
  • 2,673
  • 1
  • 16
  • 27
4
votes
3 answers

how to pass lambda expression with arguments as parameters in Java 8?

Here is what I tried. and it does not even compile. public class LambdaExample { public static Integer handleOperation(Integer x, Integer y, Function converter){ return converter.apply(x,y); } public static void…
brain storm
  • 30,124
  • 69
  • 225
  • 393
4
votes
2 answers

Generic FunctionalInterface and Method Reference messed up by Type Erasure

I have the following generic FunctionalInterface: @FunctionalInterface public interface FooInterface { void bar(T arg); } And this ArrayList descendant: public class FooList extends ArrayList> { public void doFoo(T…
3
votes
1 answer

Lambda expression executes correctly but anonymous class definition throws error

I am trying to understand the Consumer interface of Java. I have replicated it. But it throws StackOverflowError when i replace the lambda expression in return statement of andThen() method with anonymous class definition: interface Interface { …
3
votes
1 answer

Why is it compiled? - T = Supplier?

I have an 'Animal' interface and 'Dog' class that implements 'Animal'. public interface Animal { void makeVoice(); } public class Dog implements Animal { @Override public void makeVoice() { System.out.println("aou aou"); …
3
votes
1 answer

Base interface for a functional interface

I recently came across this when trying to provide common functionality to a sub-class of a functional interface. Apparently you cannot do it the way I thought (atleast for Java 8): @FunctionalInterface public interface Base> { …
voidnull
  • 53
  • 4
3
votes
1 answer

How is it possible to dynamically convert a method object into a functional interface in Java?

I have a few classes that each implement an interface. From these classes I search out a method using an annotation. This method returns a boolean and always has an object as parameter, which always inherits from another fixed object. Now I want to…
3
votes
1 answer

How to write Composable function using Kotlin Functional (SAM) Interface

We can write functional interfaces in Kotlin like this - function-interfaces fun interface Sum { fun add(a: Int, b: Int): Int } val sumImpl = Sum { a, b -> return@Sum a + b } val testSum = sumImpl.add(4, 5) How can we write Jetpack…
3
votes
2 answers

Use property getter method reference for functional (SAM) interface variables

I want to pass a property getter method reference as one of the function arguments, and have that argument be of my own functional interface type, but ran into an issue. Here's a stripped down minimal reproducible case, I changed the variable from…
Quinteger
  • 133
  • 1
  • 8
3
votes
1 answer

Java - Why are SAM types with matching signature not interchangable?

Java can correctly deduce SAM types and allows me to pass a lambda as a substitute to their implementation, but the same mechanism fails when I try to convert one SAM type to another that has the same signature: public static Supplier
zombieParrot
  • 111
  • 6
3
votes
2 answers

Consumer with varargs

Suggest the following: Consumer consumer = (args) -> { /* Do something */ } To make use of the consumer, I have to create an Object array. So, I'd have to write something like consumer.accept(new Object[]{ object0, object1, ...…
3
votes
0 answers

Check if function passed to a method marked with annotation

Is there a way to check if function passed to a method marked with annotation or no? Here is a code snippet below for better understanding what I want to get. public class AnnotationExample { static void method(Function f) { // how to…
3
votes
3 answers

Which might be the downsides of declaring Functional Interfaces as variables in Java?

I have been experimenting with Functional Programming in Java for a while now, and noticed that I started to prefer the use of the @FunctionalInterface functions from the java.util.function package such as Functions, BiFunctions, UnaryOperators,…