国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java8中Stream的使用示例教程

java8中Stream的使用示例教程

2021-06-09 13:50jihite Java教程

Stream是Java8的一大亮點,是對容器對象功能的增強,下面這篇文章主要給大家介紹了關于java8中Stream使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來

前言

java8中提供了stream對集合操作作出了極大的簡化,學習了stream之后,我們以后不用使用for循環就能對集合作出很好的操作。

本文將給大家詳細介紹關于java8 stream使用的相關內容,下面話不多說了,來一起看看詳細的介紹吧

1. 原理

stream 不是集合元素,它不是數據結構并不保存數據,它是有關算法和計算的,它更像一個高級版本的 iterator。

原始版本的 iterator,用戶只能顯式地一個一個遍歷元素并對其執行某些操作;

高級版本的 stream,用戶只要給出需要對其包含的元素執行什么操作,比如:

  • 所有元素求和
  • 過濾掉長度大于 10 的字符串
  • 獲取每個字符串的首字母

stream 就如同一個迭代器(iterator),單向,不可往復,數據只能遍歷一次,遍歷過一次后即用盡了,就好比流水從面前流過,一去不復返。

而和迭代器又不同的是,stream 可以并行化操作

stream 的另外一大特點是,數據源本身可以是無限的

2.使用步驟

獲取一個數據源(source)→ 數據轉換→執行操作獲取想要的結果

每次轉換原有 stream 對象不改變,返回一個新的 stream對象(可以有多次轉換),這就允許對其操作可以像鏈條一樣排列,變成一個管道,如下圖所示。

java8中Stream的使用示例教程

3. stream的構造

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void test4() {
 stream stream = stream.of("a", "b", "c", 23);
 stream.foreach(key -> system.out.println(key));
 
 string[] array = new string[]{"abc", "efg"};
 stream = stream.of(array);
 stream = arrays.stream(array);
 stream.foreach(key -> system.out.println(key));
 
 list<string> list = arrays.aslist(array);
 stream = list.stream();
 
 //intstream、longstream、doublestream
 intstream stream2 = intstream.of(1, 2, 3, 3);
 doublestream stream4 = doublestream.of(1, 2, 3, 3.4);
 
 stream2.foreach(key -> system.out.println(key));
 stream4.foreach(key -> system.out.println(key));
 }

結果

a
b
c
23
abc
efg
1
2
3
3
1.0
2.0
3.0

4. stream的轉換

?
1
2
3
4
5
6
7
8
9
10
11
public void test6() {
 stream stream = stream.of("abc", "def");
 
 string[] array = (string[])stream.toarray(string[]::new);
 system.out.println(array.length);
 list<string> list = (list<string>)stream.of("1", "2", "3").collect(collectors.tolist());
 string str = stream.of("abc", "mn").collect(collectors.joining()).tostring();
 system.out.println(array);
 system.out.println(list);
 system.out.println(str);
 }

結果

2

[ljava.lang.string;@17f052a3
[1, 2, 3]
abcmn

5.一個 stream 只可以使用一次

?
1
2
3
4
5
public void test6_5() {
 stream stream = stream.of(1, 2, 3, 2);
 system.out.println("count:" + stream.count());
 system.out.println("count:" + stream.count());
}

輸出

exception in thread "main" java.lang.illegalstateexception: stream has already been operated upon or closed
    at java.util.stream.abstractpipeline.<init>(abstractpipeline.java:203)
    at java.util.stream.longpipeline.<init>(longpipeline.java:91)
    at java.util.stream.longpipeline$statelessop.<init>(longpipeline.java:572)
    at java.util.stream.referencepipeline$5.<init>(referencepipeline.java:221)
    at java.util.stream.referencepipeline.maptolong(referencepipeline.java:220)
    at java.util.stream.referencepipeline.count(referencepipeline.java:526)
    at streamtest.streamtest.test6_5(streamtest.java:68)
    at streamtest.streamtest.main(streamtest.java:181)
count:4

6.轉換大寫

?
1
2
3
4
5
6
7
8
9
public void test7() {
 list<string> list = arrays.aslist("a", "mnm");
 
 list<string> result = list.stream().
 map(string::touppercase).
 collect(collectors.tolist());
 system.out.println(list);
 system.out.println(result);
 }

輸出

[a, mnm]
[a, mnm]

7.平方

?
1
2
3
4
5
6
7
8
9
public void test8() {
 list<integer> list2 = arrays.aslist(1, 2, 4);
 list<integer> list3 = list2.stream().
 map(key -> key * key).
 collect(collectors.tolist());
 system.out.println(list2);
 system.out.println(list3);
 
 }

輸出

[1, 2, 4]
[1, 4, 16]

8.找偶數

?
1
2
3
4
5
6
7
8
public void test8_5() {
 list<integer> list2 = arrays.aslist(1, 2, 4);
 list<integer> list3 = list2.stream().
 filter(key -> key % 2 == 0).
 collect(collectors.tolist());
 system.out.println(list2);
 system.out.println(list3);
 }

輸出

[1, 2, 4]
[2, 4]

9. 區間值

?
1
2
3
4
5
6
public void test5() {
 system.out.println("\n");
 intstream.range(1, 3).foreach(system.out::println);
 system.out.println("\n");
 intstream.rangeclosed(1, 3).foreach(system.out::println);
 }

結果

1
2
 
 
1
2
3

10.并發

?
1
2
3
public void test5_pa() {
intstream.rangeclosed(1, 10).parallel().foreach(system.out::println);
}

輸出

3
7
1
5
2
8
10
6
9
4  

是否并發思考

11. 新的stream繼續操作

?
1
2
3
4
5
6
7
8
public void test6_6() {
 stream.of("one", "two", "three", "four")
 .filter(e -> e.length() > 3)
 .peek(e -> system.out.println("filtered value: " + e))
 .map(string::touppercase)
 .peek(e -> system.out.println("mapped value: " + e))
 .collect(collectors.tolist());
 }

結果

filtered value: three
mapped value: three
filtered value: four
mapped value: four

12. optional

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void print(string text) {
 system.out.println("<<<<<<");
 system.out.println(optional.ofnullable(text));
 list<string> obj = new arraylist<>();
 optional.ofnullable(text).ifpresent(system.out::println);
 system.out.println(">>>>>>>>>>>>\n");
 }
 public static int getlength(string text) {
 return optional.ofnullable(text).map(string::length).orelse(-1);
 }
 
 public void test14() {
 string stra = " abcd ", strb = null;
 print(stra);
 print("");
 print(strb);
 
 system.out.println(getlength(stra));
 system.out.println(getlength(""));
 system.out.println(getlength(strb));
 }

結果

<<<<<<
optional[ abcd ]
 abcd
>>>>>>>>>>>>
 
<<<<<<
optional[]
 
>>>>>>>>>>>>
 
<<<<<<
optional.empty
>>>>>>>>>>>>
 
6
0
-1

13. 字符串拼接、最值、求和、過濾

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void test15() {
 string concat = stream.of("a", "b", "c").reduce("", string::concat);
 system.out.println("concat:" + concat);
 
 double minvalue = stream.of(-1.5, 1.0, -3.0, -2.0).reduce(double.max_value, double::min);
 system.out.println("min:" + minvalue);
 
 int sumvalue = stream.of(1, 2, 3, 4).reduce(0, integer::sum);
 system.out.println("sum1:" + sumvalue);
 
 int sumvalue2 = stream.of(1, 2, 3, 4).reduce(integer::sum).get();
 system.out.println("sum2:" + sumvalue2);
 
 concat = stream.of("a", "b", "c", "d", "e", "f").filter(x -> x.compareto("z") > 0).reduce("", string::concat);
 system.out.println("concat:" + concat);
 }

結果

concat:abc
min:-3.0
sum1:10
sum2:10
concat:ace

14. limit, skip

?
1
2
3
4
5
6
public void test16() {
 list<person> persons = new arraylist<>();
 intstream.range(1, 1000).foreach(key->persons.add(new person(key, "jihite:" + key)));
 list<string> personlist = persons.stream().map(person::getname).limit(10).skip(3).collect(collectors.tolist());
 system.out.println(personlist);
 }

輸出

[jihite:4, jihite:5, jihite:6, jihite:7, jihite:8, jihite:9, jihite:10]

15.找出最長一行的長度

?
1
2
3
4
5
6
7
8
9
10
public void test19() throws ioexception {
 string path = "**/person.java";
 bufferedreader br = new bufferedreader(new filereader(path));
 int longest = br.lines()
 .maptoint(string::length)
 .max()
 .getasint();
 br.close();
 system.out.println(longest);
 }

輸出

 

16.找出全文的單詞,轉小寫,并排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void test20() throws ioexception {
 string path = "**/person.java";
 bufferedreader br = new bufferedreader(new filereader(path));
 list<string> words = br.lines()
 .flatmap(line->stream.of(line.split(" ")))
 .filter(word->word.length()>0)
 .map(string::tolowercase)
 .distinct()
 .sorted()
 .collect(collectors.tolist());
 br.close();
 system.out.println(words);
 words.foreach(key-> system.out.println(key));
 }

輸出

*
*/
/**
//
2018/10/24
21:40
=
@author:
@date:
@description:
class
getname()
int
name)

參考

java 8 中的 streams api 詳解

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/kaituorensheng/p/9852462.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲第十页 | 黄色影片免费在线观看 | 久久久国产一区二区三区 | 午夜爽爽爽 | 日韩视频在线免费观看 | 免费av观看| 欧美精品第一页 | 精品国产一区二区三区免费 | 91精品国产91久久综合 | 久久国产精品久久 | 狠狠ri| 国产精品高清在线观看 | 国产91在线观看 | 欧美精品成人一区二区三区四区 | 日韩毛片免费看 | 国产精品1区2区 | 91久久| 国产精品高清在线 | 日韩欧美一区二区精品 | 国产精品一区二区av | 国产成人精品免高潮在线观看 | 国产高清在线精品一区二区三区 | 激情久久久久 | 九九热视频在线 | 精品视频在线视频 | 美女视频黄色 | 成人在线国产 | 一区二区三区视频免费在线观看 | av一区二区在线观看 | 日本中文一区二区 | 在线婷婷 | 九九热视频精品在线观看 | 久久青青 | 国内精品久久久久久中文字幕 | 黄色片在线免费观看 | 日日夜夜伊人 | 成人免费视频7777777 | 最新电影在线高清免费完整观看视频 | 国产欧美综合一区二区三区 | 热久久影院| 在线播放高清视频www |