Why Functional Interfaces in Java 8 have one Abstract Method?

The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method. For more details refer here. Edit -> Also worth noting here is that, a … Read more

How can Comparator be a Functional Interface when it has two abstract methods? [duplicate]

The docs also state: If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. And since equals is one of those methods, the “abstract method … Read more

Why do I need a functional Interface to work with lambdas?

When you write : TestInterface i = () -> System.out.println(“Hans”); You give an implementation to the void hans() method of the TestInterface. If you could assign a lambda expression to an interface having more than one abstract method (i.e. a non functional interface), the lambda expression could only implement one of the methods, leaving the … Read more

Why does a lambda change overloads when it throws a runtime exception?

The problem is that there are two methods: void fun(Runnable r) and void fun(Supplier<Void> s). And an expression fun(() -> { throw new RuntimeException(); }). Which method will be invoked? According to JLS §15.12.2.1, the lambda body is both void-compatible and value-compatible: If the function type of T has a void return, then the lambda … Read more

Is it possible to declare that a Supplier needs to throw an Exception?

Edit As pointed many times, you don’t need any custom class, use Callable and Runnable instead Wrong, outdated solution Consider this generic solution: // We need to describe supplier which can throw exceptions @FunctionalInterface public interface ThrowingSupplier<T> { T get() throws Exception; } // Now, wrapper private <T> T callMethod(ThrowingSupplier<T> supplier) { try { return … Read more

Use method reference with parameter

You can’t use method references for this purpose. You have to resort to lambda expressions. The reason why the bind2 method of the linked question doesn’t work is that you are actually trying to bind two parameters to convert a three-arg function into a one-arg function. There is no similarly simple solution as there is … Read more

Thread.sleep inside infinite while loop in lambda doesn’t require ‘catch (InterruptedException)’ – why not?

The reason for this, is that these invocations are in fact, invocations to two different overloaded methods available in ExecutorService; each of these methods taking a single argument of different types: <T> Future<T> submit(Callable<T> task); 2. Future<?> submit(Runnable task); Then what happens is that the compiler is converting the lambda in the first case of … Read more

Precise definition of “functional interface” in Java 8

From the same page you linked to: The interface Comparator is functional because although it declares two abstract methods, one of these—equals— has a signature corresponding to a public method in Object. Interfaces always declare abstract methods corresponding to the public methods of Object, but they usually do so implicitly. Whether implicitly or explicitly declared, … Read more