Repository navigation
Expand file tree
/
Copy pathstream.html
More file actions
129 lines (99 loc) · 11.1 KB
/
Copy pathstream.html
File metadata and controls
129 lines (99 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>1.8. Stream</title><link rel="stylesheet" type="text/css" href="docbook.css" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="home" href="index.html" title="Netkiller Java 手札(版)" /><link rel="up" href="java.html" title="第 1 章 Java 14" /><link rel="prev" href="java.util.Queue.html" title="1.7. Queue" /><link rel="next" href="Optional.html" title="1.9. Optional" /></head><body><a xmlns="" href="//www.netkiller.cn/">Home</a> |
<a xmlns="" href="//netkiller.github.io/">简体中文</a> |
<a xmlns="" href="http://netkiller.sourceforge.net/">繁体中文</a> |
<a xmlns="" href="/journal/index.html">杂文</a> |
<a xmlns="" href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> |
<a xmlns="" href="https://edu.51cto.com/lecturer/1703915.html">51CTO学院</a> |
<a xmlns="" href="https://edu.csdn.net/lecturer/6423">CSDN程序员研修院</a> |
<a xmlns="" href="https://github.com/netkiller">Github</a> |
<a xmlns="" href="http://my.oschina.net/neochen/">OSChina 博客</a> |
<a xmlns="" href="https://cloud.tencent.com/developer/column/2078">腾讯云社区</a> |
<a xmlns="" href="https://yq.aliyun.com/u/netkiller/">阿里云栖社区</a> |
<a xmlns="" href="https://www.facebook.com/bg7nyt">Facebook</a> |
<a xmlns="" href="http://cn.linkedin.com/in/netkiller/">Linkedin</a> |
<a xmlns="" href="//www.netkiller.cn/home/donations.html">打赏(Donations)</a> |
<a xmlns="" href="//www.netkiller.cn/home/about.html">About</a><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">1.8. Stream</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="java.util.Queue.html">上一页</a> </td><th width="60%" align="center">第 1 章 Java 14</th><td width="20%" align="right"> <a accesskey="n" href="Optional.html">下一页</a></td></tr></table><hr /></div><table xmlns=""><tr><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe></td><td></td><td><a href="https://zhuanlan.zhihu.com/netkiller"><img src="/images/logo/zhihu-card-default.svg" height="25" /></a></td><td valign="middle"><a href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> | <a href="https://www.zhihu.com/club/1241768772601950208">多维度架构</a></td><td></td><td></td><td></td><td></td></tr></table><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="stream"></a>1.8. Stream</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695381168"></a>1.8.1. Stream.of</h3></div></div></div><pre class="programlisting">
Stream<String> stream = Stream.of("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695380400"></a>1.8.2. Stream.ofNullable</h3></div></div></div><p>增加单个参数构造方法,可为null</p><pre class="screen">
Stream.ofNullable(null).count(); // 0
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695379360"></a>1.8.3. filter</h3></div></div></div><p>filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤掉空字符串:</p><pre class="programlisting">
List<String> strings = Arrays.asList("Hollis", "", "HollisChuang", "H", "hollis");
strings.stream().filter(string -> !string.isEmpty()).forEach(System.out::println);
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695378144"></a>1.8.4. map</h3></div></div></div><p>map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:</p><pre class="programlisting">
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().map( i -> i*i).forEach(System.out::println);
//9,4,4,9,49,9,25
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695376944"></a>1.8.5. limit/skip</h3></div></div></div><p>limit 返回 Stream 的前面 n 个元素;skip 则是扔掉前 n 个元素。以下代码片段使用 limit 方法保理4个元素:</p><pre class="programlisting">
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().limit(4).forEach(System.out::println);
//3,2,2,3
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695375744"></a>1.8.6. sorted</h3></div></div></div><p>sorted 方法用于对流进行排序。以下代码片段使用 sorted 方法进行排序:</p><pre class="programlisting">
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().sorted().forEach(System.out::println);
//2,2,3,3,3,5,7
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695374592"></a>1.8.7. distinct</h3></div></div></div><p>distinct主要用来去重,以下代码片段使用 distinct 对元素进行去重:</p><pre class="programlisting">
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().distinct().forEach(System.out::println);
//3,2,7,5
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695373440"></a>1.8.8. forEach</h3></div></div></div><p>Stream 提供了方法 'forEach' 来迭代流中的每个数据。以下代码片段使用 forEach 输出了10个随机数:</p><pre class="programlisting">
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695372304"></a>1.8.9. count</h3></div></div></div><p>count用来统计流中的元素个数。</p><pre class="programlisting">
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis","Hollis666", "Hello", "HelloWorld", "Hollis");
System.out.println(strings.stream().count());
//7
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695371152"></a>1.8.10. collect</h3></div></div></div><p>collect就是一个归约操作,可以接受各种做法作为参数,将流中的元素累积成一个汇总结果:</p><pre class="programlisting">
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis","Hollis666", "Hello", "HelloWorld", "Hollis");
strings = strings.stream().filter(string -> string.startsWith("Hollis")).collect(Collectors.toList());
System.out.println(strings);
//Hollis, HollisChuang, Hollis666, Hollis
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695369792"></a>1.8.11. takeWhile 和 dropWhile</h3></div></div></div><p>增加 takeWhile 和 dropWhile 方法</p><pre class="screen">
Stream.of(1, 2, 3, 2, 1)
.takeWhile(n -> n < 3)
.collect(Collectors.toList()); // [1, 2]
</pre><p>从开始计算,当 n < 3 时就截止</p><pre class="screen">
Stream.of(1, 2, 3, 2, 1)
.dropWhile(n -> n < 3)
.collect(Collectors.toList()); // [3, 2, 1]
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695368032"></a>1.8.12. List to Stream</h3></div></div></div><pre class="programlisting">
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
Stream<String> stream = strings.stream();
</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="idm184695367200"></a>1.8.13. 混合使用的例子</h3></div></div></div><pre class="programlisting">
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
Stream s = strings.stream().filter(string -> string.length()<= 6).map(String::length).sorted().limit(3)
.distinct();
</pre></div></div><div xmlns="" id="SOHUCS"></div><script xmlns="" charset="utf-8" type="text/javascript" src="https://cy-cdn.kuaizhan.com/upload/changyan.js"></script><script xmlns="" type="text/javascript">
window.changyan.api.config({
appid: 'cyvwjQUG3',
conf: 'prod_ef966242df3d8b5acb1e0ee9fc01cafe'
});
</script><script xmlns="" type="text/javascript" id="clustrmaps" src="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM"></script><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="java.util.Queue.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="java.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="Optional.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">1.7. Queue </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 1.9. Optional</td></tr></table></div><script xmlns="">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11694057-1', 'auto');
ga('send', 'pageview');
</script><script xmlns="" async="async">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script xmlns="" async="async">
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script></body></html>