Java8-11-stream introduces the stream

This series of articles comes from the

original CSDN address:

https://blog.csdn.net/ryo1060732496/article/details/88806283

What is a stream?

Stream is a new member of the Java API, which allows you to process data collections in a declarative manner. In simple terms, you can think of it as a high-level iterator of data collections. In addition, streams can be processed in parallel transparently, so you don't need to write any multi-threaded code.

Introduction to Streams

Collections in Java 8 support a new stream method, which returns a stream (the interface is defined in java.util.stream.Stream). As you will see later, there are many other ways to get streams, such as using numerical ranges or generating stream elements from I/O resources.

So, what exactly is a stream? The short definition is a sequence of elements generated from a source that supports data processing operations.

List<Dish> menu = Dish.MENU;
//从menu获得流
List<String> threeHightCaloricDishNames = menu.stream()
    // 通过链式操作,筛选出高热量的菜肴
    .filter(d -> d.getCalories() > 300)
    // 获取菜名
    .map(Dish::getName)
    .limit(3)
    .collect(Collectors.toList());

We used a declarative way to process the menu data, and did not implement filter, map or limit functions. The Stream library has its own.

The difference between flow and collection

  1. Is it urgent

Both the existing collection concept and the new stream concept of Java provide interfaces to cooperate with the data interface representing the ordered value of the element. The so-called order means that we generally take values ​​in order, rather than randomly. What is the difference between the two?

For example, when we are watching a movie, these videos are just a stream (byte stream or frame stream). The streaming video player only needs to download the frames of the user’s viewing position in advance, so that there is no need to wait for the stream. Most of the values ​​are calculated. For example, if the progress bar of the video we watched on Youtube is dragged to a position, you will find that it will start playing soon. You don't need to load the entire video, but load a section. If we don’t follow this approach, we can imagine that the video player may not have the entire stream as a collection to save the required memory buffer-and if it has to wait until the last frame appears to start watching, then the waiting time It's too long, I'm impatient to watch it.

At first glance, the difference between a set and a stream is when the calculation is performed. A collection is an in-memory data structure that contains all the current values ​​in the data structure. Each element in the collection must be calculated before it can be added to the collection.

In contrast, a stream is a conceptually fixed data structure, and its elements are calculated on demand (lazy loading). Give as much as you need. This is a relationship between producer and consumer. From another point of view, a stream is like a delayed collection: a value is only generated when the consumer requests it. In contrast, collections are created eagerly (like scalpers stock up).

  1. Can only be traversed once
  2. External iteration and internal iteration

Guess you like

Origin blog.csdn.net/weixin_43298913/article/details/106121328