Java8 default interface methods and lambda expressions

Default interface method

Java 8 enables us to default add non-abstract method implementations to interfaces by using  keywords . This function is also called virtual extension method . This is a relatively large change. In the past, our perception was that there can only be method definitions in interfaces, but methods cannot be implemented. Now using the default keyword, the specific content of methods can be implemented in the interface, as follows sqrt:

interface Formula{

    double calculate(int a);

    default double sqrt(int a) {
        return Math.sqrt(a);
    }

}

The advantage is that when we want to expand the interface, we don't need to modify all the implementation classes of the interface one by one. Great update.

lambda expression

With lambda expressions, it is convenient for us to simplify the writing of internal objects, as follows:

/**
*以往的写法
*/
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");

Collections.sort(names, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return b.compareTo(a);
    }
});
/**
*新写法
*/
Collections.sort(names, (String a, String b) -> {
    return b.compareTo(a);
});

The simplification is not just a little bit, and the code logic is clearer. Note that when lambda expressions are written in internal classes, external variables can be called without final. However, external variables are the same as before and cannot be modified, otherwise they cannot be compiled.

Functional interface

The functional interface is an extension of the fusion lambda expression. Its definition is very simple. As long as the interface has only one method , the interface is a functional interface (there is only one method, and the function of this interface is as single as a function). java.lang.Runnable And  java.util.concurrent.Callable are the two most typical examples of functional interfaces. It does not matter whether you add @FunctionalInterface or not. Jvm can judge it. It is generally recommended to use @FunctionalInterface annotations to declare on the interface. In this case, the compiler will report an error if it finds that the interface with this annotation has more than one abstract method. The usage is as follows:

Runnable runnable=()->{
      System.out.print("这里是自定义的run方法....");
};

//对比以前的写法:
  Runnable run=new Runnable() {
            @Override
            public void run() {
                
            }
        };

It is very convenient to write.

Stream

Java 8 extends the collection class. You can create a Stream through Collection.stream() or Collection.parallelStream() to java.util.Stream represent the sequence of operations that can be applied to a set of elements at one time. To put it bluntly, you can do some operations on a set of elements of the Collection type, such as filtering, sorting, matching, etc. It should be noted that the returned value is a copy of the operation and has no effect on the original data, so as to filter ( Filter) as an example:

    //这个是操作的list,元素假设是[a1,b1,c1,d1]
     stringList
                .stream()
                .filter((s) -> s.startsWith("a"))
                .forEach(System.out::println);//a1

 

Guess you like

Origin blog.csdn.net/u013821237/article/details/90370736