Talk about Stream in Java | JD Team

1 Introduction

insert image description here
The manufacture and manipulation of collections is essential in our day-to-day programming tasks. When we need to group or search for a collection, we need to use iterators to operate on the collection, and when we need to process a large amount of data, in order to improve performance, we need to use parallel processing, such a processing method is very complicated. Streams can help developers save valuable time and make all of the above easier.
insert image description here

2 Stream Introduction

insert image description here

What exactly is flow? A brief definition is "a sequence of elements generated from a source that supports data processing operations", and a brief analysis of this definition follows.

2.1 Support data processing operations

Stream data processing operations are similar to databases that can specify grouping or search in a declarative manner, and are consistent with the idea of ​​functional programming, such as filter, map, reduce, find, match, sort, etc. These stream operations can be executed serially , can also be executed in parallel.

2.2 source

The stream will use a source that provides data. There are three ways to create an object stream. One is to create a stream from a collection object:



List<Integer> list = Arrays.asList(111,222,333);
Stream<Integer> stream = list.stream();

One is to create a stream from an array:

IntStream stream = Arrays.stream(new int(){
    
    111,222,333});

One is to create a stream by the static method Stream.of(), and the underlying layer is Arrays.stream():

`Stream<Integer> stream = Stream.of(111, 222, 333);`
Stream stream = Stream.of(111, 222, 333);

The original order is preserved when generating a stream from an ordered collection. A stream generated from a list whose elements are in the same order as the list.
There are also two special streams:

  • Empty stream: Stream.empty()
  • Infinite Stream: Stream.genarate()

2.3 Element sequence

Streams can also access a set of ordered values ​​​​containing specific element types like collections, but their main purpose is different. The main purpose of collections is to store and access elements, and the main purpose of streams is to express calculations.

3 streams of thought

The idea of ​​streaming is similar to the pipeline in production. Many streaming models return a stream. These models are only responsible for what they need to do, and do not require extra memory space to store the processing results. These flow models can be linked together to form a large pipeline. In this process, we do not pay attention to how the data in the intermediate steps are processed, and only need to use the processed results of the entire pipeline. The following code can reflect this idea. In the code, we take commodities as an example. We need to filter out the names of the first two commodities whose volume is greater than 200.
The first is the definition of the commodity class:

public class Goods {
    
    
    private final String Name;
    private final Integer Volume;

    public Goods(String name, Integer volume) {
    
    
        Name = name;
        Volume = volume;
    }
    public String getName() {
    
    
        return Name;
    }
    public Integer getVolume() {
    
    
        return Volume;
    }
}

Next is the definition of the commodity collection:

List<Goods> goods = Arrays.asList(new Goods("土豆",10),
new Goods("冰箱",900),new Goods("办公椅",300));

Next get the result we want:

   List<String> twofoods = goods.stream()//获取流
    .filter(goods1 -> goods1.getVolume()>200)//筛选商品体积大于200的
    .map(Goods::getName)//获取商品名称
    .limit(2)//筛选头两个商品
    .collect(Collectors.toList());//将结果保存在list中

From this point of view, isn't it much more convenient to use streams to handle our specific needs than to use collection iteration?

4 Characteristics of stream processing

  • no data stored
  • does not change the data source
  • can only be used once

Here we use a test class StreamCharacteristic to verify the above characteristics of stream processing:

import org.springframework.util.Assert;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamCharacteristic {
    
    
    public void test1(){
    
    
        List<Integer> list = Arrays.asList(1,2,2,5,6,9);
        list.stream().distinct();
        System.out.println(list.size());
    }
    public void test2(){
    
    
        List<String> list = Arrays.asList("wms", "KA", "5.0");
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
        stream.forEach(System.out::println);
    }
}

The result in test1() is 6. Although we have performed the deduplication operation distinct() on the Stream generated by the list object, it does not affect the data source list.

In test2(), the stream.forEach method is called twice to print each word. When the second call is made, a "java.lang.IllegalStateException" is thrown: "stream has already been operated upon or closed". This means that the stream does not store data, the stream has been consumed after traversal, and the stream cannot be reused.

5 Stream operations and use of streams

Connecting all stream operations can be combined into a pipeline, and the pipeline has two types of operations: intermediate operations and terminal operations.

The commonly used intermediate operations of StreamAPI are: filter, map, limit, sorted, distinct.
The commonly used terminal operations of StreamAPI are: forEach, count, collect.

When using streams, three elements are mainly required: a data source for executing queries, an intermediate operation chain for forming a stream pipeline, and a terminal operation that can execute the pipeline and generate results.

The following figure shows the entire operation process of the stream:
insert image description here

6 Summary
insert image description here

  • A stream is a sequence of elements produced from a source that supports data processing operations
  • The idea of ​​flow is similar to the assembly line in production
  • The stream does not store data, does not change the data source, and can only be changed once
  • Stream operations are mainly divided into two categories: intermediate operations and terminal operations.

Guess you like

Origin blog.csdn.net/leader_song/article/details/131840806