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 create Stream in Java 8

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Java 8 how to create Stream, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Stream.of variable parameter Stream stream1 = Stream.of ("A", "B", "C"); System.out.println ("stream1:" + stream1.collect (joining ()

Program output:

Stream1:ABC

2. Stream.of array String [] values = new String [] {"A", "B", "C"}; Stream stream2 = Stream.of (values); System.out.println ("stream2:" + stream2.collect (joining ()

Program output:

Stream2:ABC

Look at the Stream.of source code, the above two ways are actually the packaging version of the third way.

Public static Stream of (T... Values) {return Arrays.stream (values);}

The way we use the source code directly is the same.

3. Arrays.streamString [] values = new String [] {"A", "B", "C"}; Stream stream3 = Arrays.stream (values); System.out.println ("stream3:" + stream3.collect (joining ()

Program output:

Stream3:ABC

4. ListList list = Arrays.asList ("A", "B", "C"); Stream stream4 = list.stream (); System.out.println ("stream4:" + stream4.collect (joining ()

Program output:

Stream4:ABC

5. SetSet set = new HashSet (Arrays.asList ("A", "B", "C"); Stream stream5 = set.stream (); System.out.println ("stream5:" + stream5.collect (joining ()

Program output:

Stream5:ABC

6. MapMap map = new HashMap (); map.put ("1", "A"); map.put ("2", "B"); map.put ("3", "C"); Stream stream6 = map.values (). Stream (); System.out.println ("stream6:" + stream6.collect (joining ()

Program output:

Stream6:ABC

7. Stream.iterateStream stream7 = Stream.iterate ("A", e-> String.valueOf ((char) (e.charAt (0) + 1)) .limit (3); System.out.println ("stream7:" + stream7.collect (joining ()

Program output:

Stream7:ABC

8. PatternString value = "A B C"; Stream stream8 = Pattern.compile ("\\ W") .splitAsStream (value); System.out.println ("stream8:" + stream8.collect (joining ()

Program output:

Stream8:ABC

9. Files.linestry {Stream stream9 = Files.lines (Paths.get ("d:/data.txt")); System.out.println ("stream9:" + stream9.collect (joining ();} catch (IOException e) {e.printStackTrace ();}

The data.txt file is as follows:

ABC

Program output:

Stream9:ABC

10. Stream.generateStream stream10 = Stream.generate (()-> "A"). Limit (3); System.out.println ("stream10:" + stream10.collect (joining ()

Program output:

Stream10:AAA

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report