Implementing an interface with two abstract methods by a lambda expression

Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the modifiers to default or static and override them within the classes that implement them if necessary. public class Test { public static void main(String[] args) { I1 i1 = … Read more

Callable vs Supplier interface in java

Their difference in usage can be seen from their respective documentation: Callable: A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. … Read more

FunctionalInterface Comparator has 2 abstract methods

equals() is not an abstract method. This method overrides Object.equals(Object), and is there only for the Comparator interface to be able to have javadoc attached to the method, explaining how comparators should implement equals(). See the javadoc of FunctionalInterface: If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that … Read more

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

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

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

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