Why isn’t @FunctionalInterface used on all the interfaces in the JDK that qualify?

Well, an annotation documenting an intention would be useless if you assume that there is always that intention given. You named the example AutoCloseable which is obviously not intended to be implemented as a function as there’s Runnable which is much more convenient for a function with a ()->void signature. It’s intended that a class … Read more

When and why would you use Java’s Supplier and Consumer interfaces?

The reason you’re having difficulty grasping the meaning of functional interfaces such as those in java.util.function is that the interfaces defined here do not have any meaning! They are present primarily to represent structure, not semantics. This is atypical for most Java APIs. The typical Java API, such as a class or interface, has meaning, … 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 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

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

What are functional interfaces used for in Java 8?

@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface. But you can use lambdas without this annotation as well as you can override … Read more