Date Api

Java8 introduced LocalDate and LocalDateTime to handle date handling, and a new class DateTimeFormatter to handle date formatting.

The default method of the interface

Java8 can be implemented by adding non-abstract methods to the interface using the default keyword.

interface Formula{

    double calculate(int a);

    default double sqrt(int a) {
        returnMath.sqrt(a); }}Copy the code

Formula interface in addition to abstract method calculation interface Formula also defines the default method SQRT. The class that implements this interface only needs to implement the abstract method Calculate. The default method SQRT can be used directly. Of course, you can also create objects directly through the interface, and then implement the default methods in the interface. Let’s demonstrate this in code.

public class Main {

  public static void main(String[] args) {
    // Access the interface through an anonymous inner class
    Formula formula = new Formula() {
        @Override
        public double calculate(int a) {
            return sqrt(a * 100); }}; System.out.println(formula.calculate(100));     / / 100.0
    System.out.println(formula.sqrt(16));           / / 4.0}}Copy the code

Formulas are implemented as anonymous objects. The code is very easy to understand, and six lines of code implement the calculation of SQRT (a * 100). In the next section, we’ll see that there is a better and more convenient way to implement a single method object in Java 8.

Both abstract classes and interfaces can be accessed through anonymous inner classes. You cannot create objects directly through abstract classes or interfaces. An inner class implements an abstract method in the interface and returns an inner class object, which we then refer to with a reference to the interface.


The parallel flow ParalleStream


        Stream<Integer> integerParallelStreamList = List.of(1.2.3).parallelStream();

        Stream<String> stringParallelStreamList = List.of("1"."2"."3").parallelStream();
Copy the code

In general, we don’t need to use parallel streams. If the number of elements in a Stream is less than a thousand, performance will not improve much, because there is a cost to dividing elements between different cpus

Stateless method

The execution of stateless methods does not depend on the result set executed by previous methods.

Map (mapToInt, mapToLong, etc.), filter, flatMap

flatMap

This method is used to flatten a pair of multiple elements:

        List<Order> orders = List.of(new Order(), new Order());
        Stream<Item> itemStream = orders.stream()
                .flatMap(order -> order.getItemList().stream());
Copy the code

For example, if we have an order and each order contains a List of goods, we need to use the flatMap method if we want to combine all the List of goods in the two orders into a new List of goods.

Stateful method

Stateful methods are methods whose execution depends on the result set of previous methods

The method name Methods the results
distinct() Elements to heavy
sorted() Element sort, overloaded two methods, can pass in a sort object when needed.
limit(long maxSize) Pass in a number that takes only the first X elements
skip(long n) Pass in a number that represents skipping X elements and taking the next element.

1. Connect the stream

If you construct two streams in two places and want to use them together, you can use concat() :

        Stream<Integer> concat = Stream
                .concat(Stream.of(1.2.3), Stream.of(4.5.6));
Copy the code