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

A summary of the parameters and return type of functional interfaces in the package java.util.function

I'm looking for a table of parameters and return type of the single abstract method (SAM) of all interfaces in java.util.function.
6
votes
2 answers

What is a "function shape" with respect to functional interfaces in Java 8?

In Java 8 the new package java.util.function contains a lot of functional interfaces. The documentation for that package (http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html) makes several references to "function…
skomisa
  • 16,436
  • 7
  • 61
  • 102
5
votes
2 answers

Java Lambda Expressions and Method References to Generic Methods

I have a functional interface import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; @FunctionalInterface public interface SubmitterCompletable extends Submitter { @Override CompletableFuture
stonar96
  • 1,359
  • 2
  • 11
  • 39
5
votes
2 answers

Java Functional Interfaces and Lambda Expressions

Given the following code, can someone please explain why the assertion returns true? Despite having searched around countlessly, I haven't been able to get any appropriate answer for why this might be the case, and what the Java feature(s) cause…
Vilitaria
  • 107
  • 9
5
votes
2 answers

Consumer and Consumer seems not fully equivalent - same with any Consuming functional interface

I am having a java compiler error that I don't understand. It seems that Consumer< ? > and Consumer< T > (with T extends Object) are not equivalent in method signature arguments. Please check out the code below: import…
5
votes
3 answers

Is it possible to use generic Consumer as function argument in Java?

Code: public void ifChangedString(String key, Consumer consumer) { ... consumer.accept(getString(key)); } public void ifChangedBoolean(String key, Consumer consumer) { ... consumer.accept(getBoolean(key)); } Is it…
Drunya
  • 121
  • 1
  • 7
5
votes
2 answers

BiConsumer and method reference of one parameter

Why is it legal to pass a method reference of one parameter as an argument of expected type BiConsumer whose abstract method requires two arguments? Example: class Experiment { private String name; public Experiment(String name) { …
5
votes
1 answer

Is lambda in fact an anonymous class?

I am reading Effective Java and wondering about differences between lambda and anonymous class. I know that lambda can only be used with interfaces with single method i.e. Functional Interfaces and in lambda you cannot obtain reference to itself so…
5
votes
1 answer

Why does this.getClass give it's own class name rather than Anonymous class name?

I have created anonymous class by implementing interface I inside public static void main() method. So, by java 8 for the abstract method test(), the implementation is provided from imple() method of class C. So, inside public static void main()…
5
votes
2 answers

Lambda Expression is not working, getting terminated

wrote java8 program with lambda expression, its not getting executed instead its getting terminated at the lambda expression, no exceptions import java.util.ArrayList; import java.util.List; import java.util.function.BiConsumer; …
user1245524
  • 285
  • 1
  • 8
  • 18
5
votes
3 answers

Understanding lambdas and/or predicates

I'm completely new to Java 8 and I'm trying to wrap my head around why the last test is false. @Test public void predicateTest() { Predicate test1 = p -> 1 == 1; Predicate test2 = p -> p == (1==1); …
tisaksen
  • 329
  • 4
  • 10
5
votes
1 answer

Is a class being instantiated in a lambda expression?

I have the following method invocation, in which I am passing a lambda expression. Is a class being instantiated implicitly here? printStudents( roster, (Student s) -> s.getGender() == Student.Sex.MALE && s.getAge() >= 18 &&…
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
5
votes
4 answers

How to pass a Lambda to toSortedSet() in Kotlin

I'm a little confused why this doesn't work. I'm having a simple Iterable of String that I want to sort via toSortedSet() my own way. I wanted to pass a lambda to it like this: myStringIterable.toSortedSet({a,b -> a.compareTo(b)}) That, however,…
Jan B.
  • 6,030
  • 5
  • 32
  • 53
5
votes
4 answers

How come two method references are compiled into two different addresses?

I reference the same method twice but the references are different. See this example: import java.util.function.Consumer; public class MethodRefTest { public static void main(String[] args) { new MethodRefTest(); } public…
tobywoby
  • 85
  • 5
5
votes
1 answer

how to implements a java SAM interface in Kotlin?

In Java it is possible to write code like this: model.getObservableProduct().observe(this, new Observer() { @Override public void onChanged(@Nullable ProductEntity productEntity) { model.setProduct(productEntity); …
0leg
  • 13,464
  • 16
  • 70
  • 94