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

Java: functional chaining of getters or zero args methods

I want to create Comparator based on Enum.name() which is inside a bean class: List.of(new JobRsp(Job.MY), new JobRsp(Job.OUR), new JobRsp(Job.YOUR)).stream() .sorted(__ -> __.getJob().name()); If I only needed Enum.order() I could write…
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
3
votes
1 answer

Is there a JDK functional interface that takes no arguments, returns nothing and allows checked exceptions?

I'm looking in the JDK for a functional interface with a method signature like this @FunctionalInterface public interface Executable { void execute() throws Exception; } But surprisingly, I can't seem to find one.
Antonio Dragos
  • 1,973
  • 2
  • 29
  • 52
3
votes
1 answer

Error when trying to define Functional (SAM) interfaces

In Kotlin, i'm trying to define an Interface with only one method. When trying to the following code: fun interface someInterFace { fun someFunc() } I'm getting the following error message: "expecting function name or receiver type" Can…
Eitanos30
  • 1,331
  • 11
  • 19
3
votes
5 answers

Mapping an object using a List>

I have a List> and need to map/transform a String by passing it through the list of operators. I have the following functional Java 11 code: UnaryOperator addA = (startString) -> startString + "a"; UnaryOperator
Ram
  • 865
  • 1
  • 8
  • 20
3
votes
0 answers

Comprehensive list of all functional interfaces in JDK outside java.function package

The java.function package contains many functional interfaces introduced in Java 8 that are extensively used in the Streams API. But I know for a fact that by definition there are potentially many other functional interfaces that either already…
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
3
votes
3 answers

How is Comparator a Functional Interface?

According to definition of functional interface - A functional interface is an interface that contains only one abstract method. But Comparator has two abstract methods: int compare(T o1, T o2); boolean equals(Object obj); others are default…
Jashan Chahal
  • 291
  • 2
  • 8
3
votes
1 answer

How to detect class difference at compile time using generics (java)

I am using java 8 and I would like to detect subtle class differences at compile time modifying withProperty() header. This code is working but I would like to force a compilation error in main() function because this::getInteger returns an Integer…
3
votes
1 answer

Why can't I cast the result of the super call directly?

So I have a interface that extends BiConsumer. @FunctionalInterface public interface MyActionWithParameters extends BiConsumer, P> { @Override default MyActionWithParameters andThen(BiConsumer, ?…
User1291
  • 7,664
  • 8
  • 51
  • 108
3
votes
3 answers

Does scala cache conversions to functional interfaces

Scala 2.12 can automatically convert a lambda expression to an interface. E.g, I'm using: import org.apache.kafka.common.serialization.{Deserializer, Serde, Serializer} import scalapb.GeneratedMessageCompanion class ProtoSerde[A <:…
3
votes
2 answers

From Anonymous class to lambda expression

When using an anonymous class below , the variable x us called without problem interface Age { int x = 21; void getAge(); } class AnonymousDemo { public static void main(String[] args) { Age oj1 = new Age() { …
CheBoss
  • 41
  • 1
  • 7
3
votes
1 answer

incompatible types: Object is not a functional interface netbeans-11 java

I'm quite new to lambda expression of Java 8 I tried the following and get compile error class Test{ static interface MySupplier { Object supply(); } public static void main(String[] args) { Object v = value(); …
harunaga
  • 141
  • 1
  • 1
  • 10
3
votes
1 answer

Java: Map storing lambdas as values

I want to learn possibilities within "newer" syntax and API of Java. By newer I mean 10+ (let's say 10-13). It is mostly around declaration of lambdas and storing different implementations conforming to same signature as values in the map. As…
Marek Pulka
  • 111
  • 1
  • 5
3
votes
1 answer

How to get arguments from an instance of functional interface object passed as lambda?

@FunctionalInterface public interface ServiceCaller { void callService(); } //common method to execute any service call public void executeService(ServiceCaller serviceCaller) { //do common things //i want to access…
zamiurratul
  • 201
  • 1
  • 2
  • 12
3
votes
3 answers

How to replace my old Java code with Optionals (Functional programming)?

How to replace the below code using Java 8 Optionals (Functional programming)? ClassA classA = dbService.findByA(a); if (classA == null) { classA = dbService.findByB(b); } if (classA == null) { throw new Exception(); } return…
0seNse0
  • 43
  • 5
3
votes
3 answers

How to fix "not a functional interface" problem in my code

I am learning Java predicates and struck here on basic code. Below is my code. package TestLambda; public class Animal { private String species; private boolean canHop; private boolean canSwim; public Animal(String species,boolean…
Gaurav
  • 61
  • 1
  • 1
  • 6