In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the characteristics and use of Stream in JAVA8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
I. the use of Stream
1.1 creation
1.2 steps
II. Characteristics of Stream
III. Intermediate operation
3.1 filter ()
3.2 limit ()
3.3 skip ()
3.4 map ()
3.5 sorted
IV. Terminate the operation
4.1 allMatch
4.2anyMatch
4.3noneMatch
4.4 findFirst ()
4.5 findAny ()
4.6 count
4.7 max
4.8 min
4.9 forEach
4.10 reduce
4.11 collect
1. Creation of Stream using 1.1
The stream () method provided through the implementation class of the Collection interface, or
Get through the static method stream () in Arrays
Through the static method of () in the Stream class
Infinite flow (iteration / generation)
/ * * @ Author: Youbo * @ Date: 23:28 on 2021-9-1 * streaming operation * / public class StreamTests {@ Test public void test () {/ / 1. The stream () method provided through the implementation class of the Collection interface, or Collection list = new ArrayList (); list.stream (); list.parallelStream (); / / 2. Get Integer [] integers = new Integer [10]; Arrays.stream (integers); / / 3 through the static method stream () in Arrays. Through the static method of () Stream stream = Stream.of ("1", "2") in the Stream class; / / 4. Infinite flow / / iterative Stream iterate = Stream.iterate (0, (x)-> x + 2); / / generate Stream generate = Stream.generate (()-> Math.random ());} 1.1.1 parallel flow parallelStream
ParallelStream provides parallel processing of streams, which is another important feature of Stream, which is implemented underneath using the Fork/Join framework. Simple understanding is an implementation of multithreaded asynchronous tasks.
1.2 steps
Create Stream
Convert Stream, each time the original Stream object does not change, a new Stream object is returned (there can be multiple conversions)
Reduce the Stream to get the desired results
II. Characteristics of Stream
Lazy evaluation:
Multiple intermediate operations can be connected to form a pipeline, and no processing will be performed unless the termination operation is triggered on the pipeline! Instead, it is processed all at once when the operation is terminated, which is called "lazy evaluation".
III. Intermediate operation
Screening and slicing
3.1 filter ()
Accept lambda expressions to exclude certain elements from the flow
@ Testpublic void test2 () {/ / get an array ArrayList arrayList = new ArrayList (); for (int I = 0; I num > 5) .forEach (System.out::println);} / / result: 6 7 8 93.2 limit ()
Cut off the flow so that the number of elements does not exceed a certain number
After the number of limit is satisfied, it will be short-circuited and the subsequent operation will not be performed.
@ Testpublic void test2 () {/ / get an array ArrayList arrayList = new ArrayList (); for (int I = 0; I num > 5) .limit (2) .forEach (System.out::println);} / / result: 673.3 skip ()
Skip elements, skip the first n elements, execute the following elements, and return empty flow if there are less than n
@ Testpublic void test2 () {/ / get an array ArrayList arrayList = new ArrayList (); for (int I = 0; I num > 5) .skip (2) .forEach (System.out::println);} / / result: 893.3 map () mapping, using method Function in the method
< T>Functional interface-> R apply (T t); @ Testpublic void test4 () {/ / get a list List list = Arrays.asList ("aaa", "bbb", "ccc"); / / convert uppercase list.stream () .map ((str)-> str.toUpperCase ()) .forEach (System.out::println) using stream operation } / * result: AAA BBB CCC*/@Testpublic void test3 () {/ / get a list List list = Arrays.asList ("aaa", "bbb", "ccc"); / / flow operation: take out the elements in list / / the first step is to use map to extract the stream, and the stream is still stored in the stream / / so the secondary foreach Stream chs = list.stream () .stream (StreamTests::getUpper) is required. Chs.forEach ((stream)-> {stream.forEach (System.out::print);});} / / returns str as a stream object public static Stream getUpper (String str) {List list = new ArrayList (); for (Character character: str.toCharArray ()) {list.add (character);} return list.stream ();} / / result: aaabbbccc3.4 map ()
Mapping, using method Function in methods
< T>Functional interface-> R apply (T t)
@ Testpublic void test4 () {/ / get a list List list = Arrays.asList ("aaa", "bbb", "ccc"); / / convert uppercase list.stream () .map ((str)-> str.toUpperCase ()) .forEach (System.out::println) using stream operation } / * result: AAA BBB CCC*/@Testpublic void test3 () {/ / get a list List list = Arrays.asList ("aaa", "bbb", "ccc"); / / flow operation: take out the elements in list / / the first step is to use map to extract the stream, and the stream is still stored in the stream / / so the secondary foreach Stream chs = list.stream () .stream (StreamTests::getUpper) is required. Chs.forEach ((stream)-> {stream.forEach (System.out::print);});} / / returns str as a stream object public static Stream getUpper (String str) {List list = new ArrayList (); for (Character character: str.toCharArray ()) {list.add (character);} return list.stream ();} / / result: aaabbbccc3.3.1 flatMap
Equivalent to the addAll of a set method
That is, take out the elements in the flow and put them into a stream instead of entrapment within the flow.
@ Testpublic void test3 () {/ / get a list List list = Arrays.asList ("aaa", "bbb", "ccc"); / / flow operation: take out the elements in list / / the first step is to use map to extract the stream, and the stream is still stored in the stream / / so the secondary foreach Stream chs = list.stream () .stream (StreamTests::getUpper) is required. Chs.forEach ((stream)-> stream.forEach (System.out::print)); System.out.println ("\ n ="); / / method 2: / / use flatMap list.stream (). FlatMap (StreamTests::getUpper) .forEach (System.out::print);} 3.5 sorted
@ Testpublic void test5 () {List list = Arrays.asList ("aaa", "ccc", "bbbb", "eeeee"); / / Natural sort list.stream () .sorted () .forEach (System.out::println); System.out.println ("=") / / Custom sort list.stream () .sorted ((xPowery)-> {/ / if the length is the same, sort by dictionary if (x.length () = = y.length ()) {return x.compareTo (y) } / / if the length is not the same, sort else {return y.length ()-x.length ();}}) .forEach (System.out::println) in descending order of length;} / * result: aaabbbbccceeeee=eeeeebbbbaaaccc*/ IV, terminate the operation
Find and match
4.1 allMatch
Predicate
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.