This is another new feature of Java 8. The emergence of Stream is more like the progress of Java with the development of The Times. The development of big data promotes the progress of Java and the emergence of Stream programming.

Before streams, if we wanted to iterate over a collection and manipulate it, we would use a for loop or an iterator. Stream is the same as the iterator function in front of, in fact is to traverse the entire collection, but Stream provides more API, developers can more convenient to deal with the data in the program memory.

For code demonstration purposes, create an Apple entity

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Apple {
    private String name;
    private int weight;
}
Copy the code

Now we have the following requirements:

  • Give you A collection of physical apples, take out the apple named “A” and print it out
public static void main(String[] args) {
    List<Apple> apples = Arrays.asList(
        new Apple("A".30),
        new Apple("B".20),
        new Apple("C".60));
    // Do not use Stream
    for (Apple apple : apples) {
        if ("A".equals(apple.getName())) { System.out.println(apple); }}// Use Stream to simplify code
    apples.stream().
        filter(a -> "A".equals(a.getName())).
        forEach(System.out::println);
}
Copy the code

As you can see, the length of the code is the same, but the semantics of the code are clearer.

At this time you may still have doubts, I do not understand the following code ah, how can say that the code expression ability is stronger.

‘s the time to explain the meaning of the Stream, spoken in front of the Stream is a more advanced traversal, in fact, we can also think of it like a river, all the data in the river is flowing, once you slipped (water) after processing, will not return to the original place, we operate the Stream is such a process.

Stream is then used in conjunction with lambda expressions.

To help you use Stream better, here is a demo of the common Stream apis

public static void main(String[] args) {
    List<Apple> apples = Arrays.asList(
        new Apple("A".30),
        new Apple("B".20),
        new Apple("C".60));
    
    // Filter the apple with the name "A" and print it
    apples.stream().
        filter(a -> "A".equals(a.getName())).
        forEach(System.out::println);
    // Result: Apple(name=A, weight=30)
    
    // Middle operation map, change all apple names to lowercase, and print the result
    apples.stream().
        map(a -> a.getName().toLowerCase()).
        forEach(System.out::println);
    // result: a, B, C
    
    // Terminate count to count the number of apples in the list that weigh more than 25
    long count = apples.stream().
        filter(a -> a.getWeight() > 25).
        count();
    System.out.println(count);
    // Result: 2
    
    // Stop operation min and find the apple with the smallest weightSystem.out.println(apples.stream(). min((a1, a2) -> a1.getWeight() - a2.getWeight()). get().getName()); Results: B// Terminate operation MaxSystem.out.println(apples.stream(). min((a1, a2) -> a2.getWeight() - a1.getWeight()). get().getName()); Result: C}Copy the code

This is the end of Stream programming. There are many other features of Stream programming that I’ve just covered here.

That’s all for today

Stay hungry. Stay foolish. See you next time