How do Java 8 default methods hеlp with lambdas?

mCs :

It is claimed in this article that:

one of the major reasons for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.

I could understand that the @FunctionalInterface helped by saying that there is ONLY one abstract method and the lambda should represent this particular method.

But how is it that the default methods helped to support lambdas?

Fullstack Guy :

To give you an example take the case of the Collection.forEach method, which is designed to take an instance of the Consumer functional interface and has a default implementation in the Collection interface:

default void forEach(Consumer<? super T> action) {
    Objects.requireNonNull(action);
    for (T t : this) {
        action.accept(t);
    }
}

If the JDK designers didn't introduce the concept of default methods then all the implementing classes of the Collection interface would have to implement the forEach method so it would be problematic to switch to Java - 8 without breaking your code.

So to facilitate the adoption of lambdas and the use of the new functional interfaces like Consumer, Supplier, Predicate, etc. the JDK designers introduced the concept of default methods to provide backward compatibility and it is now easier to switch to Java - 8 without making any changes.

If you don't like the default implementation in the interface you can override it and supply your own.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=35163&siteId=1