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

Example Analysis of Stream flow Operation in Java8

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you the content of a sample analysis of Stream stream operations in Java8. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1 Stream stream

When you iterate over a collection, you can call its iterator method, return an iterator object, and then traverse the elements in the collection through the iterator object, which is called an external iteration (the for loop itself encapsulates its syntax sugar). The diagram is as follows:

In addition, there are internal iterative methods, which is a series of operations on the Stream object returned by the stream () method of the collection to be explained here, for example, to count the even elements of a list of numbers, when using the operation of the Stream object, as follows:

List list = new ArrayList () {{add (1); add (2); add (3);}}; long count = list.stream (). Filter (num-> num% 2 = = 0). Count (); System.out.println (count); / / 1

The schematic diagram is as follows:

The example provided above, such as filter, takes an lambda expression as its argument, so Stream is actually a tool for performing complex operations on collection classes in a functional programming manner.

2 Stream stream operation and Spark RDD operator

In fact, people with Spark experience will have a sense of deja vu when they start using Stream streaming operations, as if everything is so familiar.

Let's make a simple comparison from the point of view of operation object (noun) and object operation (verb).

2.1 Operand

The operating object of the Spark RDD operator is RDD, which means elastic distributed data set in Chinese. For users, it is a collection-like object, which contains data, but at the bottom, its data may be distributed in each partition of each node, but in any case, its essence is still the data set.

The operation object of Stream stream operation is a collection, which is also a kind of data set in nature, but it is stand-alone compared to RDD.

2.2 object manipulation

There are two types of Spark RDD operators, namely the Transformation operator and the Action operator. The former is delayed calculation, it only remembers the logical operation of the data, but does not really execute, and the latter is the calculation that really triggers the Transformation operator.

There are also two types of Stream stream operations, namely lazy evaluation and early evaluation (I think this translation is not good). The former only records the logical operation of lazy evaluation, while the latter is the real trigger operation.

We can see that the two are very similar, one is all kinds of operations on distributed data, the other is various operations on stand-alone data, and the calculation is divided into delayed calculation and trigger calculation. The benefit is obvious: when performing multiple logical operations on the data set, it is possible that the iteration can be completed only once, so when the calculation is really triggered, the performance improvement brought by one iteration is significant. For example, filtering and calculating these two operations (the previous operation of calculating even numbers) can be done in one iteration.

Of course, not only are the types similar, but the names of the operations they provide are similar, and some things are really generic.

3 common stream operations

Each operation is illustrated with an easy-to-understand example.

3.1 early evaluation operation 3.1.1 collect (toList ())

Its function is to collect the elements in the Stream stream to form List, Set, Map, and so on.

List list = Stream.of (1, 2, 3). Collectors.toList (); System.out.println (list); / / [1, 2, 3]

The 1.Stream.of () method is used to easily generate Stream streams

2.Collectors also has toSet (), toMap () and other methods. For more information, please see its API.

3.1.2 forEach (Consumer)

Operate on each element in the collection, whose argument is the Consumer function interface.

Consumer printNum = System.out::print;Stream.of (1,2,3) .forEach (printNum); / / 123

System.out::print means to use the print method in the System.out class, which is equivalent to the lambda expression: element-> System.out.print (element)

The above example can also be achieved in one step:

Stream.of (1,2,3) .forEach (System.out::print); / / 1233.1.3 max and min

Its parameter is a Comparator object, which returns an Optional object, and Optional indicates that the result may or may not be there (for example, when operating on a Stream stream with a null value).

/ / calculate the maximum value in the numeric stream Optional maxOptional = Stream.of (1,2,3) .max (Comparator.comparing (num-> num)); System.out.println (maxOptional.get ()); / / 3pm / find the string Optional minOptional = Stream.of ("a", "ab", "abc") .min (Comparator.comparing (String::length)); System.out.println (minOptional.get ()); / / a

In addition, it is indeed an early evaluation operation, which can be verified:

Stream.of (1,2,3) .max (Comparator.comparing (num-> {System.out.println (num); return num;}))

Output:

12233.2 lazy evaluation operation 3.2.1 map

Its parameter is Function, which is used to convert the value in the Stream stream to another stream.

/ / convert letters to uppercase Stream.of ("a", "b", "hello") .map (String::toUpperCase) .forEach (element-> System.out.print (element + "")); / / A B HELLO3.2.2 filter

Its parameter is Predicate, which filters the elements in the Stream stream.

/ / find the even number List list = Stream.of (1,2,3) .filter (num-> num% 2 = = 0) .filter (Collectors.toList ()); System.out.println (list); / / [2] 3.2.3 flatMap

Its parameter is Function, except that R is limited to Stream at this time, converting the values in the Stream stream into more streams.

/ / find the words List list = Stream.of ("hello you", "hello me") .flatMap (line-> Arrays.stream (line.split (") .flatMap (Collectors.toList ()); System.out.println (list); / / [hello, you, hello, me]

Does it feel a bit like Spark's wordcount example?

3.2.4 reduce

Its parameter is BinaryOperator and an Optional object is returned. Optional indicates that the result may or may not exist (for example, when operating on a Stream stream with a null value, and no initial value is specified) for reduction operations.

/ / Sum Integer res = Stream.of (1,2,3). Reduce ((acc, element)-> acc + element). Get (); / / after specifying the initial value of 6, the reduce operation result of Stream must have a value, so it does not return Optional, but directly the type to which 6 belongs, that is, IntegerInteger res2 = Stream.of (1,2,3). Reduce (6, (acc, element)-> acc + element) System.out.println (String.format ("res:% s, res2:% s", res, res2)); / / res: 6, res2: 12 Thank you for reading! This is the end of the article on "sample Analysis of Stream flow Operation in Java8". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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