A note on the use of stream in Java 8

Introduction to stream

Let's take a look at how stream is defined in Java:

A sequence of elements supporting sequential and parallel aggregate operations.

Let's interpret the above sentence:

You can think of stream as an advanced version of iterator. In the original version of iterator, users can only traverse elements one by one and perform some operations on them; In the advanced version of stream, users only need to give what operations they need to perform on the elements contained, such as "filter out strings with a length greater than 10", "get the first letter of each string", etc. How to apply these operations to each element, just give it to stream! (I won't tell ordinary people about this secret script:)) after reading these, you may not have an intuitive understanding of stream. Don't worry, let's have a piece of code.

The above code is to get the number of non null elements in a list. Although this code is very short, it is a good entry-level example to reflect how to use stream. It is the so-called "sparrow is small and has five dirty parts".

Introduction

An NPE was encountered this afternoon, as shown in the figure below

Follow the source code and finally the referencepipeline #193 line. It should be that the input parameter u is null when calling apply, thus triggering a null pointer exception.

To test my idea, I wrote the following test examples:

Best practices

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

reference material

Java se 8 best practices]( https://www.jfokus.se/jfokus17/preso/Java-SE-8-best-practices.pdf )

Flow processing line: referencepipeline]( http://download.java.net/lambda/b72/docs/api/java/util/stream/ReferencePipeline.html )

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>