什么是 Stream?
Stream(流)是一个来自数据源的元素队列并支持聚合操作
元素是特定类型的对象,形成一个队列。 Java中的Stream并不会存储元素,而是按需计算。
数据源 流的来源。 可以是集合,数组,I/O channel, 产生器generator 等。
聚合操作 类似SQL语句一样的操作, 比如filter, map, reduce, find, match, sorted等。
和以前的Collection操作不同, Stream操作还有两个基础的特征:
Pipelining: 中间操作都会返回流对象本身。 这样多个操作可以串联成一个管道, 如同流式风格(fluent style)。 这样做可以对操作进行优化, 比如延迟执行(laziness)和短路( short-circuiting)。
内部迭代: 以前对集合遍历都是通过Iterator或者For-Each的方式, 显式的在集合外部进行迭代, 这叫做外部迭代。 Stream提供了内部迭代的方式, 通过访问者模式(Visitor)实现。
- 创建.of()
// 创建Stream of
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
stream.forEach(System.out::print); // 输出结果为 : 123456
Stream<String> stream = Stream.of("a", "b", "c", "d", "e");
stream.forEach(System.out::print);
- 限制/跳过.limit().skip()
// 限制 limit
Stream<String> stream = Stream.of("a", "b", "c", "d", "e");
stream.limit(3).forEach(System.out::print); // 输出结果为 : abc
// 跳过 skip
stream = Stream.of("a", "b", "c", "d", "e");
stream.skip(3).forEach(System.out::print); // 输出结果为 : de
- 过滤.filter()
Stream<String> stream = Stream.of("a", "bb", "", "", "e");
System.out.print(stream.filter(s -> !s.isEmpty()).collect(Collectors.toList())); // 输出结果为 : [a, bb, e]
- 去重.distinct()
Stream<String> stream = Stream.of("a", "a", "c", "d", "d");
stream.distinct().forEach(System.out::print); // 输出结果为 : acd
- 映射.map()
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
- 窥视.peek()
Stream<String> stream = Stream.of("a", "b", "c", "d", "e");
System.out.println(stream.peek(v -> System.out.print(v)).map(String::toUpperCase).collect(Collectors.toList())); // 输出结果为 : abcde[A, B, C, D, E]
- 排序.sort()
Stream<String> stream = Stream.of("a", "c", "e", "b", "d");
System.out.println(stream.sorted(Comparator.comparing(String::toString)).collect(Collectors.toList())); // 输出结果为 : [a, b, c, d, e]
- 最大值/最小值.max().min()
Stream<String> maxStream = Stream.of("a", "b", "d", "e", "c");
System.out.println(maxStream.max(Comparator.comparing(String::toString)).get()); // 输出结果为 : e
Stream<Integer> minStream = Stream.of(6, 2, 1, 6, 8, 12);
System.out.println(minStream.min(Comparator.comparing(Integer::intValue)).get()); // 输出结果为 : 1
- 匹配.match()
Stream<String> stream = Stream.of("a", "c", "e", "b", "d");
// Stream中全部元素符合传入的Predicate,返回true
System.out.println(Boolean.valueOf(stream.allMatch(p -> "e".equals(p)))); // 输出结果为 : false
stream = Stream.of("a", "c", "e", "b", "d");
// Stream 中只要有一个元素符合传入的Predicate,返回true
System.out.println(Boolean.valueOf(stream.anyMatch(p -> "e".equals(p)))); // 输出结果为 : true
stream = Stream.of("a", "c", "e", "b", "d");
// Stream 中没有一个元素符合传入的Predicate,返回true
System.out.println(Boolean.valueOf(stream.noneMatch(p -> "e".equals(p)))); // 输出结果为 : false
10.收集.collect()
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println("筛选列表: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);
- 集合.reduce()
Stream<String> stream = Stream.of("a", "b", "c", "d", "e");
// 追加
System.out.println(stream.reduce(String::concat).get()); // 输出结果为 : abcde
stream = Stream.of("aa", "bb", "ccc", "d", "eee");
// 求最长
System.out.println(stream.reduce((s1, s2) -> s1.length()>=s2.length() ? s1 : s2).get()); // 输出结果为 : ccc
stream = Stream.of("aa", "bb", "ccc", "d", "eee");
// 单词长度之和
System.out.println(stream.reduce(0, (sum, str) -> sum+str.length(), (a, b) -> a+b)); // 输出结果为 : 11
// stream.mapToInt(str -> str.length()).sum();