Been writing middleware-related code, provided SDK to use the business side, but a lot of the business side has been stuck at version 1.7, delay the upgrade, for compatibility, not to use some of the new features Java8 in the code, such as Stream and the like, although it can not use, but still have to learn about.
What is Stream
Stream is a new feature added in Java 8, it java.io package of InputStream and OutputStream are completely different concepts. It is by means of Lambda expressions, allows you to process the data in a declarative way, it can greatly improve the productivity of Java programmers, allowing programmers to write efficient, clean, simple code.
Stream Demo
Directly on the Demo, feel
List<String> myList = Arrays.asList("a", "b", "c", "d", "e");myList.stream() .filter(s -> s.startsWith("1")) .map(String::toUpperCase) .sorted() .forEach(System.out::println);
How to Work Stream
When a stream of time, generally includes three basic steps:
Obtaining a data source (source)
Data Conversion
Perform operations to obtain the desired results
Stream per-conversion original object does not change, a new Stream object returns (there may be multiple conversions), which allows the same as the chain may be arranged in operation thereof, into a pipe, as shown in FIG.
In Stream, the operation is divided into two
Intermediate operation
End
Stream operation returns to the intermediate, terminal or a non-void return operation result Stream, in the demo, filter
, map
, sorted
are considered intermediate operation, and forEach
is an end operation.
How to generate Stream
创建Stream的方式很多,最常见的是从Collections,List 和 Set中生成
List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");Stream<String> stream = myList.stream()
在对象myList上调用方法 stream() 返回一个常规对象Stream。
也可以从一堆已知对象中生成。
Stream<String> stream = Stream.of("a1", "a2", "a3")
当然了,还有其它方式:
Collection.stream()
Collection.parallelStream()
BufferedReader.lines()
Files.walk()
BitSet.stream()
Random.ints()
JarFile.stream()
....
常规操作
forEach
forEach
方法接收一个 Lambda 表达式,用来迭代流中的每个数据
Stream.of(1, 2, 3).forEach(System.out::println);// 1// 2// 3
map
map
用于映射每个元素到对应的结果
Stream.of(1, 2, 3).map( i -> i*i).forEach(System.out::println);// 1// 4// 9
filter
filter
用于通过设置的条件过滤出元素
Stream.of(1, 2, 3).filter( i -> i == 1).forEach(System.out::println);// 1
limit
limit
用于用于获取指定数量的流
Stream.of(1, 2, 3, 4, 5).limit(2).forEach(System.out::println);// 1// 2
sorted
sorted
用于对流进行排序
Stream.of(4, 1, 5).sorted().forEach(System.out::println);// 1// 4// 5
Match
有三个 match 方法,从语义上说:
allMatch:Stream 中全部元素符合传入的 predicate,返回 true
anyMatch:Stream 中只要有一个元素符合传入的 predicate,返回 true
noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true
它们都不是要遍历全部元素才能返回结果。例如 allMatch 只要一个元素不满足条件,就 skip 剩下的所有元素,返回 false。
boolean result = Stream.of("a1", "a2", "a3").allMatch(i -> i.startsWith("a"));System.out.println(result);// true
reduce
reduce
方法根据指定的函数将元素序列累积到某个值。此方法有两个参数:
起始值
累加器函数。
如果有一个List,希望得到所有这些元素和一些初始值的总和。
int result = Stream.of(1, 2, 3).reduce(20, (a,b) -> a + b);System.out.println(result);// 26
collect
Collectors类中提供了功能丰富的工具方法
toList
toSet
toCollection
toMap
...
而这些方法,都需要通过 collect
方法传入。
Set<Integer> result = Stream.of(1, 1, 2, 3).collect(Collectors.toSet());System.out.println(result);// [1, 2, 3]
collect
可以把Stream数据流转化为Collection对象,
骚技巧
for循环
除了常规的对象Stream,还有一些有特殊类型的Stream,用于处理基本数据类型int、long和double,它是IntStream、LongStream和DoubleStream。
比如可以使用IntStream.range()来代替常规的for循环。
IntStream.range(1, 4).forEach(System.out::println);
随机数
Random的ints方法可以返回一个随机数据流,比如返回1到100的10个随机数。
Random random = new Random();random.ints(1, 100).limit(10).forEach(System.out::println);
大小写转化
List<String> output = wordList.stream() .map(String::toUpperCase) .collect(Collectors.toList());
Stream 特点
总之,Stream 的特性可以归纳为:
无存储
Stream并不是一种数据结构,它只是某种数据源的一个视图
安全性
对Stream的任何修改都不会修改背后的数据源,比如对stream执行过滤操作并不会删除被过滤的元素,而是会产生一个不包含被过滤元素的新Stream。
惰式执行
Stream上的操作并不会立即执行,只有等到用户真正需要结果的时候才会执行。
一次性
Stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成。
lambda
所有 Stream 的操作必须以 lambda 表达式为参数