Java 简明教程

Java - Stream API Improvements

引入 Stream 到 Java 中,以帮助开发者从一系列对象中执行聚集操作。对于 Java 9,添加了一些方法使 Stream 变得更好。

Streams were introduced in Java to help developers perform aggregate operations from a sequence of objects. With Java 9, few more methods are added to make streams better.

takeWhile(Predicate Interface) Method

Syntax

default Stream<T> takeWhile(Predicate<? super T> predicate)

takeWhile 方法获取所有值,直到谓词返回 false。对于有序流,它返回一个由从该流中提取的最长元素前缀组成、与给定谓词相匹配的流。

takeWhile method takes all the values until the predicate returns false. It returns, in case of ordered stream, a stream consisting of the longest prefix of elements taken from this stream matching the given predicate.

Example

package com.tutorialspoint;

import java.util.stream.Stream;

public class Tester {
   public static void main(String[] args) {
      Stream.of("a","b","c","","e","f").takeWhile(s->!s.isEmpty())
         .forEach(System.out::print);
   }
}

takeWhile 方法获取所有的 a、b 和 c 值,然后一旦字符串为空,它便停止执行。

takeWhile method takes all a, b, and c values, then once string is empty, it stopped executing.

abc

dropWhile(Predicate Interface)

Syntax

default Stream<T> dropWhile(Predicate<? super T> predicate)

dropWhile 方法丢弃所有值,直到谓词返回 true。对于有序流,它返回一个由从该流中删除与给定谓词相匹配的最长元素前缀后的剩余元素组成流。

dropWhile method throw away all the values at the start until the predicate returns true. It returns, in case of ordered stream, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements matching the given predicate.

Example

package com.tutorialspoint;

import java.util.stream.Stream;

public class Tester {
   public static void main(String[] args) {
      Stream.of("a","b","c","","e","f").dropWhile(s-> !s.isEmpty())
         .forEach(System.out::print);

      System.out.println();
      Stream.of("a","b","c","","e","","f").dropWhile(s-> !s.isEmpty())
         .forEach(System.out::print);
   }
}

dropWhile 方法删除 a、b 和 c 值,然后一旦字符串为空,它便获取所有值。

dropWhile method drops a,b and c values, then once string is empty, it takes all the values.

ef
ef

iterate Method

Syntax

static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)

iterate 方法现在将 hasNext 谓词作为参数,它在hasNext谓词返回 false 时停止循环。

iterate method now has hasNext predicate as parameter which stops the loop once hasNext predicate returns false.

Example

package com.tutorialspoint;

import java.util.stream.IntStream;

public class Tester {
   public static void main(String[] args) {
      IntStream.iterate(3, x -> x < 10, x -> x+ 3).forEach(System.out::println);
   }
}
3
6
9

ofNullable

Syntax

static <T> Stream<T> ofNullable(T t)

引入 ofNullable 方法以防止 NullPointerException 和避免对流进行 null 检查。此方法返回一个包含单个元素(如果非空)的顺序流,否则返回一个空流。

ofNullable method is introduced to prevent NullPointerExceptions and to avoid null checks for streams. This method returns a sequential Stream containing single element, if non-null, otherwise returns an empty Stream.

Example

package com.tutorialspoint;

import java.util.stream.Stream;

public class Tester {
   public static void main(String[] args) {
      long count = Stream.ofNullable(100).count();
      System.out.println(count);

      count = Stream.ofNullable(null).count();
      System.out.println(count);
   }
}
1
0