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

How to use Stream stream in Jdk8

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use Stream stream in Jdk8. It is very detailed and has a certain reference value. Friends who are interested must finish it!

1. Experience the benefits of Stream first

Requirements: give you an ArrayList to save students' scores and let you print out scores greater than 60.

Public static void main (String [] args) {ArrayList arrList = new ArrayList (); for (int I = 0; I

< 100; i++) { arrList.add((int) (Math.random() * 100)); } printValue1(arrList); } 解决方案一: 当然就是遍历这个ArrayList,然后使用if判断一下,如果其大于60,就将其输出,代码如下: private static void printValue1(ArrayList arrList) { for (Integer i : arrList) { if (i >

60) {System.out.printf ("% d", I);}

Solution 2:

Using Stream stream operation requires only one line of code

/ * * use Stream operation * * @ param arrList * / private static void printValue2 (ArrayList arrList) {arrList.stream () .filter (I-> I > 60) .forEach (System.out::println);}

two。 What is a Stream stream?

The concept of stream stream is introduced in Jdk1.8. This "stream" is different from the input and output streams in IO. It is a class in Jdk. The specific location is: java.util.stream.Stream

There are mainly three kinds of operations about it: get stream, intermediate operation, and final operation.

2.1 how do I get the stream?

The so-called get stream is to convert other objects (non-Stream objects) into Stream objects. There are only two types of objects that can be converted to Stream objects, namely:

Array (the elements in this array must be of reference type)

Integer [] iArr = {12, 14, 15, 15, 17, 19, 22}; Stream stream1 = Stream.of (iArr)

Set

List list = new ArrayList (); Stream stream = list.stream ()

2.2 Intermediate operation (a new Stream object is returned)

When we get the stream object from above, we can operate on the Stream object, and we can perform this operation indefinitely before performing the end operation. The source code of this class can be seen in the development tool, and it mainly has the following operations:

If you are careful, you will find that the parameters in most of the methods of this class are all functional interfaces (see the previous article), so this is why you can use Lambda expressions

Map converts the value of one type to another and returns a new Stream

/ / replace the strings in the collection with uppercase Stream stream0 = Stream.of ("a", "b", "hello") .map (new Function () {@ Override public String apply (String s) {return s.toUpperCase ();}}); / / the above code can be abbreviated to the following format Stream stream = Stream.of ("a", "b", "hello") .map (s-> s.toUpperCase ()) using the Lambda expression

Therefore, please be sure to understand the operation of Lambda expressions

Filter traverses the data and checks and filters the elements in it

/ filter Stream stream1 = Stream.of ("a", "abc", "abcdefg") .filter (value-> value.length () > 1) in the string collection; flatMap can replace values with Stream, and then concatenate multiple Stream into a Stream, replacing each element of the previously generated Stream stream with a new Stream object. Stream stream2 = Stream.of (1,2) .flatMap (numbers-> Stream.of (5,6,6,7,8))

In the Stream generated by the above code, 1Jing 2 will be replaced with 5JM 6, 7pm, 8pm, 5pm, 6pm, 7pm, 8pm, and 5pm.

Other common operations are:

Stream.limit (5) / / restriction. Skip (1) / / Skip (1) / / Skip (

2.3 final operation

The final operation is to achieve the desired results, including printing, converting to other objects (mainly collections, and subclass objects of functional interfaces), and so on. Can only be executed once, after the execution is closed, no other operations can be performed.

Reduce is generally used to calculate the accumulation, as follows

/ / get the cumulative value. The first parameter of reduce is the initial value Integer count = Stream.of (1,2,3). Reduce (0, (o1, O2)-> o1 + O2); System.out.println (count); / / 6

Collect converts the stream to another form. Parameters are static methods that are passed into Collectors, such as the following:

Set collect = stream.collect (Collectors.toSet ()); List collect2 = stream.collect (Collectors.toList ()); HashSet collect1 = stream.collect (Collectors.toCollection (HashSet::new)); List list = Stream.of (1,2) .cake (Collectors.toList ())

ForEach traverses the elements in this stream object

Stream.of (1,2) .forEach (I-> System.out.print (I)); System.out.println (); / / the above format can simplify Stream.of (1,2) .forEach (System.out::print) using methods referenced by static methods.

The above is all the content of the article "how to use Stream streams in Jdk8". 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