您好,欢迎访问一九零五行业门户网

Java 9中Stream API添加了哪些新功能?

in java 9, oracle corporation has added four useful new methods to stream api. those methods are iterate(), ofnullable(), takewhile() and dropwhile().
iterate()the iterate() can be used as stream version replacement of traditional for-loops. this method has improved by adding another parameter, the predicate interface that allows us to stop these endless numbers based on conditions defined with the predicate interface.
exampleimport java.util.stream.stream;public class streamiteratemethodtest { public static void main(string[] args) { stream.iterate(1, i -> i < 5, i -> i + 1).foreach(system.out::println); // iterate() }}
输出1234

ofnullable()ofnullable()方法在元素不为null时返回一个stream对象。否则,返回一个空的stream。
示例import java.util.stream.stream;public class streamofnullablemethodtest { public static void main(string[] args) { string str = "tutorialspoint"; stream.ofnullable(str).foreach(system.out::println); // ofnullable() method }}
输出tutorialspoint
takewhile()the parameter passed to a takewhile() method is a predicate interface. this method takes an element of stream object from left to right until the condition of the predicate object is no longer fulfilled.
exampleimport java.util.stream.stream;public class streamtakewhilemethodtest { public static void main(string[] args) { stream.of(1, 2, 3, 4, 5) .takewhile(i -> i < 5) // takewhile() method .foreach(system.out::println); }}
输出1234

dropwhile()传递给 dropwhile() 方法的参数也是一个 predicate 接口。它与 takewhile() 方法相反。该方法从左到右依次传递流对象中的每个元素,并忽略满足条件的所有元素。一旦条件不再满足,它将返回所有剩余的元素。
示例import java.util.stream.stream;public class streamdropwhilemethodtest { public static void main(string[] args) { stream.of(3, 2, 1, 4, 6, 7, 8, 9, 10) .dropwhile(i -> i < 5) // dropwhile() method .foreach(system.out::println); }}
输出678910
以上就是java 9中stream api添加了哪些新功能?的详细内容。
其它类似信息

推荐信息