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

What are the skills of Stream API in Java8

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

Share

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

This article introduces the relevant knowledge of "What are the skills of Stream API in Java 8". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Stream Profile

Java 8 introduces a new Stream API. The Stream here is different from the I/O stream, it is more like a collection class with Iterable, but the behavior and collection class are different.

Stream is an enhancement to the collection object function, which focuses on a variety of very convenient and efficient aggregation operations on collection objects, or large batch data operations.

3. As long as you give what operations you need to perform on the elements it contains, such as "filter out strings longer than 10","get the first letter of each string", etc., Stream will implicitly traverse internally and make corresponding data transformations.

Why use Stream?

The benefits of functional programming are particularly obvious. This code expresses more of the intent of the business logic than its implementation mechanism. Easy-to-read code is also easier to maintain, more reliable, and less error-prone.

2. High end

instance data source

Filter

1. Used when traversing data and examining elements within it.

Filter takes as an argument a function represented by a Lambda expression.

Map

1, map generated is a one-to-one mapping,for the role of

2, more commonly used

3 and very simple.

FlatMap

1. As the name implies, it is similar to map, and deeper operations

2, but there is a difference.

Map and flat return different values.

Each input element is transformed into another element according to the rules.

There are also some scenarios where there is a one-to-many mapping relationship, and flatMap is required.

5. Map 1 to 1

6. Flatmap one-to-many

7. The method declarations of map and flatMap are different

(1)Streammap(Function mapper);

(2)StreamflatMap(Function> mapper);

(3)The difference between map and flatMap: I personally think that flatMap can handle deeper data, input parameters are multiple lists, and the result can be returned as a list, while map is one-to-one, input parameters are multiple lists, and the result must be multiple lists. In layman's terms, if the input parameters are objects, then flatMap can operate on objects inside the object, while map can only operate on *** layers.

Reduce

1. It feels similar to recursion

2. Number (string) accumulation

3. Individuals haven't used it much

Collect

1. Collect generates lists, maps, and other commonly used data structures in the stream

2、toList()

3、toSet()

4、toMap()

5, custom

Optional

Optional is a new data type designed for the core class library to replace null values.

There are many complaints about the original null value, even Tony Hoare, who invented the concept, once said it was a "priceless mistake" of his own.

3, very useful, not only in lambda, where can be used

4. Optional.of(T), T is non-empty, otherwise initialization error

Optional.ofNullable(T), T is arbitrary, can be empty

isPresent(), equivalent to!= null

7. ifPresent(T), T can be a lambda expression, or other code, otherwise executed

concurrent

1. Replace stream with parallelStream or parallel

The size of the input stream is not the only factor that determines whether parallelization will bring speed gains, performance is also affected by how the code is written and the number of cores

The five factors that affect performance are: data size, source data structure, value boxing, number of CPU cores available, and time spent processing each element.

debugging

1. list.map.fiter.map.xx is a chain call, and the final call collect(xx) returns the result.

2. Lazy evaluation and early evaluation

Determining whether an operation is lazy or early is simple: just look at its return value. If the return value is Stream, then lazy evaluation; if the return value is another value or null, then early evaluation. The ideal way to use these operations is to form a chain of lazy evaluations, *** returning the desired result with an operation that evaluates early.

4. Each value can be viewed through peek, and the operation flow can be continued at the same time.

"Java 8 Stream API skills what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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