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

Convert Runnable to Supplier

How can a Runnable be converted to a Supplier? public T useSupplier(Supplier supplier) { // Does something with supplier and returns supplied value ... return value; } public void useRunnable(Runnable runnable) { // Somehow…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
6
votes
1 answer

Why Java is not complaining about an ambiguous call?

A service interface declares two methods which apparently do the same processing : interface Service { R process(Function function); T process(UnaryOperator operator); } The service above is being called…
HPH
  • 388
  • 2
  • 11
6
votes
5 answers

Real world example of using a functional interface in Java

I know a functional interface means you can have exactly/only 1 abstract method with more than 1 default method(s) but I am wondering how to relate to it with a real-world example/situation of using a functional interface in Java. Could you give a…
Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
6
votes
2 answers

Why it works: BigDecimal Sum with Reduce and BigDecimal::add

I can understand why Total1 is calculated, but as Total2 is calculated I have no idea! How can a BigDecimal::add be used in a BiFunction? Signatures are not the same !!! package br.com.jorge.java8.streams.bigdecimal; import…
6
votes
3 answers

Method reference with a full constructor call as a lambda expression in Java

I have encountered a short time ago with a competitive answer better than mine that uses a quite new method reference to me as replacement of lambda. Stream.generate(new AtomicInteger(1)::getAndIncrement)... I looked the Oracle specifications about…
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
6
votes
3 answers

Getting an interface reference inside a lambda function

Consider the following code: val hwnd = Handler() hwnd.postDelayed(object : Runnable { override fun run() hwnd.postDelayed(this, 5000) } }, 5000) This way, I can post the same Runnable to the Handler by using this (which…
Rafael
  • 3,042
  • 3
  • 20
  • 36
6
votes
3 answers

Java8: About Functional Interface

I would like to ask about the following piece of code related to Functional Interfaces. I am confused by: Rideable rider = Car :: new Is it creating a Rideable (interface) or Car (class) instance? If it is creating a Car object, the constructor new…
Patrick C.
  • 1,339
  • 3
  • 16
  • 31
6
votes
2 answers

Combine two Functions in Java8

In isReadyToDeliver method if all products in order is available (ProductState.AVAILABLE) and if order state is ready to send (OrderState.READY_TO_SEND), method must return true. I wrote both two part but I couldn't combine them in return phrase, I…
6
votes
2 answers

Better way to create a stream of functions?

I wish to do lazy evaluation on a list of functions I've defined as follows; Optional output = Stream.>> of( classA::eval, classB::eval, classC::eval) .map(f ->…
buræquete
  • 14,226
  • 4
  • 44
  • 89
6
votes
1 answer

Java Hashcode and Equals for Java 8 functional interface objects

I have some code which looks like the below: import java.util.ArrayList; import java.util.List; import java.util.function.Function; class MyObj { private final Double aDouble; public MyObj(Double aDouble) { this.aDouble = aDouble; …
6
votes
1 answer

Lambdas in FunctionalInterfaces in Java

I am trying to make use of lambdas in Java but can't understand how it works at all. I created @FunctionalInterface like this: @FunctionalInterface public interface MyFunctionalInterface { String getString(String s); } now in my code I use the…
shurrok
  • 795
  • 2
  • 13
  • 43
6
votes
1 answer

Combining functions and consumers with double-column notation

I often use the double-colon notation for brevity. I am writing the following method that takes a short list of entities, validates them, and saves back to database. @Override@Transactional public void bulkValidate(Collection
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
6
votes
1 answer

What decides which functional interface to create from a lambda?

Please consider this example: import java.util.function.Consumer; public class Example { public static void main(String[] args) { Example example = new Example(); example.setConsumer(test -> System.out.println("passed string is…
Winter
  • 3,894
  • 7
  • 24
  • 56
6
votes
1 answer

Why is the accumulator in Stream::reduce a BiFunction and not a BinaryOperator like the combiner?

Why is the accumulator argument in the Stream::reduce method a BiFunction and not a BinaryOperator like the combiner argument. Why is its type BiFunction? Why T? Should it be BiFunction?
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
6
votes
1 answer

Unit testing callback based on Consumer functional interface

I'm trying to unit test code that runs as callback in a Consumer functional interface. @Component class SomeClass { @Autowired private SomeInteface toBeMockedDependency; public method() { toBeMockedDependency.doSomething(message -> { …
Boris
  • 443
  • 8
  • 15