📅  最后修改于: 2020-10-14 00:49:20             🧑  作者: Mango
在Java 9中,改进了Stream API,并将新方法添加到Stream接口。这些方法如下表所示。
Modifier and Type | Method | Description |
---|---|---|
default Stream |
takeWhile(Predicate super T> predicate) | It returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate. Otherwise returns, if this stream is unordered, a stream consisting of a subset of elements taken from this stream that match the given predicate. |
default Stream |
dropWhile(Predicate super T> predicate) | It returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate. Otherwise returns, if this stream is unordered, a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate. |
static |
ofNullable(T t) | It returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream. |
static |
iterate(T seed, Predicate super T> hasNext, UnaryOperator |
It returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate. The stream terminates as soon as the hasNext predicate returns false. |
Stream takeWhile方法采用与其谓词匹配的每个元素。当得到不匹配的元素时,它停止。它返回包含所有匹配元素的元素子集,流的其他部分被丢弃。
在此示例中,我们有一个整数列表,并使用takewhile方法获取了偶数值。
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
List list
= Stream.of(1,2,3,4,5,6,7,8,9,10)
.takeWhile(i -> (i % 2 == 0)).collect(Collectors.toList());
System.out.println(list);
}
}
此示例返回一个空列表,因为它在第一个列表元素处失败,并且takewhile在此处停止。
输出:
[]
In this example, we are getting first two elements because these are even and stops at third element.
输出:
[2,2]
Java Stream dropWhile()方法
Stream dropWhile方法根据流元素的顺序返回结果。
有序流:在删除与给定谓词匹配的元素后,返回包含元素的流。
无序流:在删除与给定谓词匹配的元素子集之后,它返回包含该流剩余元素的流。
Java Stream dropWhile()方法示例
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
List list
= Stream.of(2,2,3,4,5,6,7,8,9,10)
.dropWhile(i -> (i % 2 == 0)).collect(Collectors.toList());
System.out.println(list);
}
}
输出:
[3, 4, 5, 6, 7, 8, 9, 10]
Java 9 Stream of Nullable方法
Stream ofNullable方法返回包含单个元素(如果非null)的顺序流。否则,它将返回一个空流。
它有助于处理空流和NullPointerException。
Java 9 Stream of Nullable方法示例1
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
List list
= Stream.of(2,2,3,4,5,6,7,8,9,10)
.dropWhile(i -> (i % 2 == 0)).collect(Collectors.toList());
System.out.println(list);
}
}
输出:
25
Stream can have null values also.
Java 9 Stream of Nullable方法示例2
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
Stream val
= Stream.ofNullable(null);
val.forEach(System.out::println);
}
}
该程序不会产生任何输出。
Java Stream迭代方法
新的重载方法iterate已添加到Java 9流接口。此方法允许我们迭代流元素直到指定条件。
它需要三个参数:seed,hasNext和next。
Java Stream迭代方法示例
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
Stream.iterate(1, i -> i <= 10, i -> i*3)
.forEach(System.out::println);
}
}
输出:
1
3
9