Java8 --- function interface and default keyword @FunctionalInterface

1. About Comparator and Comparable

As mentioned in the Comparator, then roughly speaking about the difference between Comparator and Comparable interface.

Comparator is a strategy mode, i.e., the object itself does not need to compare any changes (to implement any sort interface), but to implement sorting between objects by instantiating a Comparator strategy.

Comparable changes supports object itself (object itself implement Comparable interface), to have a sort function.

Take, for example, a set of objects need to be sorted by the sort method Collection, there are two ways, either to achieve the object Comparable interface, itself having a sort attributes; achieved either a Comparator comparator, when the transfer of the sort into.

 

Here again talk about Lambda expressions.

 

2. Lambda expressions

As we all know, Lambda expressions are inside the Java programming function, then use Lambda expressions must first meet such a condition: First, he is the type of interface, but there is one and only one abstract method. So let's look at an example using Lambda expressions.

 

Look at Java7 expression:

public class LambdaTest {

    public static void main(String[] args) {

        Say test1 = new Say() {

            @Override

            public void saySomthing(String something) {

                System.out.println(something);

            }

        };

        test2.saySomthing("hello");

}

interface Say {

    void saySomthing(String something);

}

 

The Java8 expression using Lambda expressions:

public class LambdaTest {

    public static void main(String[] args) {

        Say test2 = System.out::println;

        test2.saySomthing("hello");

    }

}

interface Say {

    void saySomthing(String something);

}

 

So similar, we have to implement a Comparator comparator would write:

 

public class LambdaTest {

    public static void main(String[] args) {

       Comparator<Book> bookComparator = Comparator.comparing(Book::getPrice);

       

       Book book1 = new Book("a", 1);

       Book book2 = new Book("b", 1);

       System.out.println(bookComparator.compare(book1, book2));

    }

}

class Book {

    private String name;

    private Integer price;

    

    // 省略constructor/getter/setter

}

 

Written completely OK, but we take a look Comparator interface source code, you may feel a little bit surprised.

It may be noted Comparator interface source code have several points:

 

The role of @FunctionalInterface annotations

Comparator interface method in a plurality (two abstract methods and a plurality of default methods)

Meaning default keyword

The following one by one to explain the above three points.

 

Role (1) @FunctionalInterface annotations

@FunctionalInterface marked on an interface, the interface is a function of the described interface.

What about the functional interface has the following characteristics:

 

There is only an abstract method

There can be multiple static methods

The method may have a plurality of default (default method)

Public abstract method can have a plurality of Object

(2) Comparator plurality of methods in interfaces (two abstract methods and a plurality of default methods)

Comparator interface can be seen that in addition to compare the abstract method, there is a method equals abstract, but as noted above, where the function interface allows public abstract Object methods.

 

Meaning (3) default keyword

default keyword interface methods can be modified default method body. When the interface implementation class that implements the interface, default keyword modified method can not achieve.

This is done in order to solve such a scenario:

Suppose there is an interface are defined as follows:

 

interface TestInterFace {

    void doSomething();

}

 

There are a lot of class that implements the interface in an application, but suddenly this interface which needs to add a new method doAnother (). At this point, all we want to achieve in the application of the implementation of this interface in plus doAnother (), this will lead to two problems: First, the modified magnitude greater contrast; the second is the realization that not all classes are required to implement doAnother () method.

The default keyword is such a problem to solve. Plus default keyword before doAnother () method, before the implementation of that interface do not realize doAnother () method is not being given. If the new implementation class implementation of this method is equivalent to the realization of this class covers methods, so that the final result is in line to run a Java polymorphism.

 

However, note that, default keyword casual use is not recommended.

Suppose there are two interfaces: InterfaceA and InterfaceB, they all have a default void test () method, and now Class C while achieving InterfaceA and InterfaceB, an error occurs when calling test () method, because it would cause ambiguity the compiler can not know whether that is a test () method C call.

————————————————

Disclaimer: This article is CSDN blogger original article "dela_", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.

Original link: https: //blog.csdn.net/dela_/article/details/8379477

 

Guess you like

Origin www.cnblogs.com/lc0605/p/11771278.html