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

Please Explain Java 8 Method Reference to instance Method using class name

Equivalent lambda expression of HighTemp::lessThanTemp is (highTemp1, highTemp2) -> { return highTemp1.lessThanTemp(highTemp2); } This is one of the features of Java8 named Reference to an Instance Method of an Arbitrary Object of a Particular Type Consider following example, interface FIface<T> { int testMethod(T a, T b); } class Test2 { private String str; Test2(String str) … 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 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

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

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