Java9新特性系列(Java9新特性系列(Stream改进))

Java8的Stream

在Java8中,一个比较大的变化就是流(Stream),具体可以看之前的一篇文章:Java8新特性系列(Stream)

Java9的Stream

Java9中Stream增加了4个方法,分别是:

  • takeWhile:在有序的Stream中,takeWhile返回从开头开始的尽量多的元素
1
2
3
4
5
6
7
8
default Stream<T> takeWhile(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
// Reuses the unordered spliterator, which, when encounter is present,
// is safe to use as long as it configured not to split
return StreamSupport.stream(
new WhileOps.UnorderedWhileSpliterator.OfRef.Taking<>(spliterator(), true, predicate),
isParallel()).onClose(this::close);
}
  • dropWhile:与takeWhile相反
1
2
3
4
5
6
7
8
default Stream<T> dropWhile(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
// Reuses the unordered spliterator, which, when encounter is present,
// is safe to use as long as it configured not to split
return StreamSupport.stream(
new WhileOps.UnorderedWhileSpliterator.OfRef.Dropping<>(spliterator(), true, predicate),
isParallel()).onClose(this::close);
}
  • ofNullable:可以创建一个单元素Stream,可以为null
1
2
3
4
public static<T> Stream<T> ofNullable(T t) {
return t == null ? Stream.empty()
: StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
}
  • iterate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
Objects.requireNonNull(f);
Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE) {
T prev;
boolean started;

@Override
public boolean tryAdvance(Consumer<? super T> action) {
Objects.requireNonNull(action);
T t;
if (started)
t = f.apply(prev);
else {
t = seed;
started = true;
}
action.accept(prev = t);
return true;
}
};
return StreamSupport.stream(spliterator, false);
}

除了对 Stream 本身的扩展,Optional和Stream之间的结合也得到了改进,可以将optional对象转化为stream对象:

1
2
3
4
5
6
List<String> list = new ArrayList<>() {{
add("a");add("b");add("c");
}};
Optional<List<String>> optional = Optional.ofNullable(list);
Stream<List<String>> stream = optional.stream();
stream.flatMap(x -> x.stream()).forEach(System.out::println);

使用举例

  • takeWhile
1
2
List<Integer> list = Arrays.asList(1, 4, 5, 2, 3, 6, 7, 8, 9, 10);
list.stream().takeWhile(x -> x < 5).forEach(System.out::println);

输出:
1
4

  • dropWhile
1
2
List<Integer> list = Arrays.asList(1, 4, 5, 2, 3, 6, 7, 8, 9, 10);
list.stream().dropWhile(x -> x < 5).forEach(System.out::println);

输出:
5
2
3
6
7
8
9
10

  • ofNullable
1
2
3
4
5
6
7
8
Stream<String> stream = Stream.of("", null);
System.out.println(stream.count());

Stream<String> stream = Stream.of(null);
System.out.println(stream.count());//会抛空指针异常

Stream<String> stream = Stream.ofNullable(null);
System.out.println(stream.count());

输出:
2
0

  • iterate
1
2
3
4
5
//java8
Stream.iterate(1, i -> i + 1).limit(5).forEach(System.out::println);

//java9
Stream.iterate(1, i -> i < 6, i -> i + 1).forEach(System.out::println);

输出:
1
2
3
4
5

坚持原创技术分享,您的支持将鼓励我继续创作!