一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

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

java8中Stream的使用示例教程

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

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

前言

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

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

1. 原理

stream 不是集合元素,它不是數(shù)據(jù)結(jié)構(gòu)并不保存數(shù)據(jù),它是有關(guān)算法和計算的,它更像一個高級版本的 iterator。

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

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

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

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

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

stream 的另外一大特點是,數(shù)據(jù)源本身可以是無限的

2.使用步驟

獲取一個數(shù)據(jù)源(source)→ 數(shù)據(jù)轉(zhuǎn)換→執(zhí)行操作獲取想要的結(jié)果

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

java8中Stream的使用示例教程

3. stream的構(gòu)造

?
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));
 }

結(jié)果

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

4. stream的轉(zhuǎn)換

?
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);
 }

結(jié)果

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.轉(zhuǎn)換大寫

?
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.找偶數(shù)

?
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. 區(qū)間值

?
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);
 }

結(jié)果

1
2
 
 
1
2
3

10.并發(fā)

?
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  

是否并發(fā)思考

11. 新的stream繼續(xù)操作

?
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());
 }

結(jié)果

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));
 }

結(jié)果

<<<<<<
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);
 }

結(jié)果

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.找出全文的單詞,轉(zhuǎn)小寫,并排序

?
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 詳解

總結(jié)

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 曹逼网站 | 国产高清路线一路线二2022 | 99r视频在线观看 | 满溢游泳池免费土豪全集下拉版 | 扒开胸流出吃奶 | 公交车揉捏大乳呻吟喘娇 | 九九精品免费视频 | 热门小说同人h改编h | 九色PORNY丨视频入口 | 日韩一区国产二区欧美三 | 小鸟酱视频在线观看 | 无人在线视频高清免费观看动漫 | 成人精品一级毛片 | 茄子香蕉视频 | 久草热8精品视频在线观看 久草草在线视视频 | 国产福利兔女郎在线观看 | 热伊人99re久久精品最新地 | 2018高清国产一道国产 | 亚洲福利天堂网福利在线观看 | 99视频福利 | 国产高清一区二区三区免费视频 | 亚洲激情综合 | 日韩精品福利视频一区二区三区 | m3u8久久国产精品影院 | 91久久偷偷做嫩草影院免费看 | 国产精品久久久久一区二区三区 | 五月色婷婷在线影院 | 国产成+人+综合+亚洲欧美丁香花 | les女同h高h喷水 | 强行扒开美女大腿挺进 | 亚洲国产精品福利片在线观看 | 岛国不卡 | 午夜私人福利影院 | 国产福利兔女郎在线观看 | caoporen在线视频入口 | 爱福利视频一区二区 | 亚洲精品成人456在线播放 | 十大网站免费货源 | 亚洲色图第四页 | 欧美精品1区 | 粗又长好猛好爽视频 |