Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java Stream

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Java Stream". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Java Stream.

Characteristics of Stream

If you want to make good use of Stream, you must find out its characteristics.

Stream is not a data structure

Although we compare Stream with the collection framework type represented by Collection, it simply extracts the data elements from the data source (Source) into the data manipulation pipeline and "flows" according to the defined rules (operators). In addition, Stream will never modify the data of the underlying data structure encapsulated by itself.

Stream is a bit like a plumbing network.

No fixed size

The water flowing in the water pipe network is of no fixed size, or even infinite. The same is true of Stream.

Inertia

Stream starts execution only if it defines a termination operation, such as collect (Collector), forEach (Consumer). The following filtering of strings that begin with h from the stream is not performed.

Stream.of ("hello", "wolrd") .filter (str- > str.startsWith ("h"))

Invariance

A given Stream is unchanged, and all intermediate operations derive a new Stream, even if the intermediate operations do not change any elements in the Stream.

Disposable

A Stream stream has only one termination operation. Once the termination operation is completed, the stream is closed. It can't be used again. It's a disposable product.

Stream stringStream = Stream.of ("1", "2"); / / forEach terminates operation prints 12 streams terminates stringStream.forEach (System.out::println); / / reuse throws IllegalStateException exception stream has already been operated upon or closed stringStream.filter (s-> s.equals ("2")) .forEach (System.out::println)

Parallel operation

Stream supports parallelization (parallel) operations, no additional multithreaded code is required, and all operations are performed in parallel automatically. But in most cases we do it serially.

How should we choose?

How do we choose between Stream and Collection? First of all, most of the scenarios in Collection can be done by Stream, or even better.

See the API operation.

They all provide a lot of methods, if you need to get the number of elements, the collection is more convenient, if you want to filter some elements, obviously, Stream's API is more convenient, and even it provides a variety of combinable operations.

Look at the initialization cost

For the collection, once the definition of use requires an one-time loading of memory, if you plan to reuse the data in memory, the use of the collection is very appropriate; and the lazy characteristics of Stream, there will not be any intermediate operations before the terminal operation, which means that the data will not be initialized to memory, which can reduce initialization costs, and even you can adjust the rate at which consumption elements are performed.

Look at the result set size

If the end result is controllable and limited, they can do both; if the result set is very large or nearly infinite, Stream will be the best choice.

Whether to change the original data

Stream does not change the original data, and Collection can do this.

Whether you want a data container or a data pipeline.

Whether object instances need to be reused

When the result is returned as Collection, we can reuse it. After a Stream is used, it is considered consumed and IllegalStateException is thrown when it is reused, as shown above.

Do you need a fixed format?

The presentation format of Stream streams is usually not as rich as the Java collection framework, which provides formats such as Set, List, Map, and so on. If you need the terminal to return to the display, obviously the collection framework is more appropriate.

In Spring MVC, Stream is represented as an array.

At this point, I believe you have a deeper understanding of "how to use Java Stream". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report