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 common methods of Stream in Java8

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

Share

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

This article mainly introduces what are the common methods of Stream in Java8. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Common methods of Java8 Stream

Stream is provided in Java8, which greatly simplifies the collection operation. After learning Stream, we can do a good job on the collection without using the for loop.

First, the initialization and transformation of the stream:

All operations of Stream in Java are stream-specific, so you must get a Stream object to use Stream:

1. Initialize a stream:

Stream stream = Stream.of ("a", "b", "c")

2. Convert the array to a stream:

String [] strArray = new String [] {"a", "b", "c"}

Stream = Stream.of (strArray)

Or

Stream = Arrays.stream (strArray)

3. Convert the collection object to a stream (Collections):

List list = Arrays.asList (strArray)

Stream = list.stream ()

Second, stream operation:

The operation of a stream can be summed up in several ways:

1. Traversal operation (map):

You can use the map operation to iterate through each object in the collection and manipulate it, and after map, you will get the collection after the operation with .operations (Collectors.toList ()).

1.1. Convert traversal to uppercase:

List output = wordList.stream ().

Map (String::toUpperCase).

Collect (Collectors.toList ())

1.2, square:

List nums = Arrays.asList (1,2,3,4)

List squareNums = nums.stream ().

Map (n-> n * n).

Collect (Collectors.toList ())

2. Filter operation (filter):

Using filter, you can filter in the object Stream, and the elements that pass the test will be left behind to generate a new Stream.

2.1.Get the String that is not empty

List filterLists = new ArrayList ()

FilterLists.add ("")

FilterLists.add ("a")

FilterLists.add ("b")

List afterFilterLists = filterLists.stream ()

.filter (s->! s.isEmpty ())

.notify (Collectors.toList ())

3. Loop operation (forEach):

If you just want to do something custom for each object in the convection, you can use forEach:

List forEachLists = new ArrayList ()

ForEachLists.add ("a")

ForEachLists.add ("b")

ForEachLists.add ("c")

ForEachLists.stream () .forEach (s > System.out.println (s))

4. Return a specific result set (limit/skip):

Limit returns the first n elements of Stream; skip discards the first n elements:

List forEachLists = new ArrayList ()

ForEachLists.add ("a")

ForEachLists.add ("b")

ForEachLists.add ("c")

ForEachLists.add ("d")

ForEachLists.add ("e")

ForEachLists.add ("f")

List limitLists = forEachLists.stream () .skip (2) .limit (3) .limits (Collectors.toList ())

Note that there is a sequential relationship between skip and limit. For example, using skip (2) will skip the first two of the collection and return c, d, e, f, and then calling limit (3) will return the first three, so the final return of cPersondPendence

5. Sort (sort/min/max/distinct):

Sort can sort all the elements in the collection. Max,min can find the largest or smallest elements in the outflow, while distinct can find elements that are not repeated:

5.1. Sort a collection:

List sortLists = new ArrayList ()

SortLists.add (1)

SortLists.add (4)

SortLists.add (6)

SortLists.add (3)

SortLists.add (2)

List afterSortLists = sortLists.stream () .sorted ((In1,In2)->

In1-In2) .resume (Collectors.toList ())

5.2. Get the element with the longest length:

List maxLists = new ArrayList ()

MaxLists.add ("a")

MaxLists.add ("b")

MaxLists.add ("c")

MaxLists.add ("d")

MaxLists.add ("e")

MaxLists.add ("f")

MaxLists.add ("")

Int maxLength = maxLists.stream () .mapToInt (s-> s.length ()) .max () .getAsInt ()

System.out.println ("the longest string length is" + maxLength)

5.3. Check the duplicate of a collection:

List distinctList = new ArrayList ()

DistinctList.add ("a")

DistinctList.add ("a")

DistinctList.add ("c")

DistinctList.add ("d")

List afterDistinctList = distinctList.stream () .distinct () .collect (Collectors.toList ())

The distinct () method can find the element equal () in stream, that is, the same element, and remove the same element.

6. Match (Match method):

Sometimes, we only need to determine whether all the conditions in the set meet the conditions, or whether there are elements in the set that meet the conditions, and we can use the match method:

All elements in allMatch:Stream match the passed predicate and return true

Return true as long as one element in the anyMatch:Stream matches the predicate passed in

None of the elements in noneMatch:Stream match the passed predicate, and true is returned.

6.1. Judge that there is no element with'c'in the collection:

List matchList = new ArrayList ()

MatchList.add ("a")

MatchList.add ("a")

MatchList.add ("c")

MatchList.add ("d")

Boolean isExits = matchList.stream () .anyMatch (s-> s.equals ("c"))

6.2. Determine whether the collection is not empty:

List matchList = new ArrayList ()

MatchList.add ("a")

MatchList.add ("")

MatchList.add ("a")

MatchList.add ("c")

MatchList.add ("d")

Boolean isNotEmpty = matchList.stream () .noneMatch (s-> s.isEmpty ())

Then false is returned.

The above is all the content of the article "what are the common methods of Stream in Java8". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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