The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
Java8 Stream Api 中的map和flatMap操作

1. preface

Java 8 Provides very useful Stream API , you can easily operate the collection. Today we will discuss two Stream intermediate operation map(Function<? super T, ? extends R> mapper) and flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)

2. map operation

map The operation is to reprocess the elements in the flow to form a new flow. This is useful in development. For example, we have a set of students, and we need to extract the age of the students from it to analyze the age distribution curve of the students.
on Java 8 Previously, we would consume the age attribute in the element by creating a new collection and then iterating the student collection. Now we have completed this requirement through a very simple streaming operation.

Schematic diagram:

stream map 操作

Corresponding pseudo-code:

 
 <>=.().(::).(.());

3. flatMap operation

Through the above example,map The operation should be very easy to understand. so flatMap What does it do? In this way, let's change the above example. If it is based on classes, extract the ages of all students in all classes to analyze the age distribution curve of students. Is it still feasible for us to use the above method at this time?

 <<>>=.().(::).(.());

Through the above operation, we can only get a set of students in each class List<List<Student>>。We also need nested loops to obtain student age data, which is very inconvenient. If we could get back the entire student collection List<Students> It's much more convenient. That's right!flatMap It can be done!

 
 <>=.().(->.().()).(::).(.());

As the pseudocode above shows, we use flatMap Bring all students together. then use map Operate to extract the age. flatMap different from map stands out with map It just extracts attributes and puts them into the stream, while flatMap first extracts attributes and puts them into a smaller stream, and then combines all the streams into one stream. There is a feeling of "gathering sand to form a tower".

Draw another picture to deepen understanding:

flatmap

4. summary

map operation and flatMap Once you are familiar with the operation, you can easily solve some data flow operation problems. To expand the knowledge, in fact, these two operations are not only present in Stream in Java 8, but actually Optional<T> These two operations also exist in, and both have similar effects.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/4444.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1
Resource download

Comments0

【最新更新】ZN7号_微盘wp二开版带余额宝功能时间盘黄金期货数字火币交易+完整数据+k线正常
[Latest update] ZN No. 7_microdisk wp second edition with balance treasure function Time disk gold futures digital hot coin trading + complete data +k line normal
Someone bought it 8 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish