语法
static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.List; public class StreamIterateMethodTest { public static void main(String args[]) { List<Integer> numbers1 = Stream.iterate(1, i -> i+1) // with two arguments .limit(10) .collect(Collectors.toList()); System.out.println("In Java 8:" + numbers1); List<Integer> numbers2 = Stream.iterate(1, i -> i <= 10, i -> i+1) // with three arguments .collect(Collectors.toList()); System.out.println("In Java 9:" + numbers2); } }
In Java 8:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] In Java 9:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
以上就是Stream API的iterate()方法在Java 9中的重要性是什么?的详细内容,更多请关注php中文网其它相关文章!