Stream API的iterate()方法在Java 9中的重要性是什么?

语法

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

Example

的中文翻译为:

示例

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.<strong>iterate</strong>(1, i -> i+1)  <strong> // with two arguments</strong>
            .<strong>limit</strong>(10)
            .<strong>collect</strong>(Collectors.toList());
      System.out.println("In Java 8:" + numbers1);
      <strong>List<Integer></strong> numbers2 = Stream.<strong>iterate</strong>(1, i -> i <= 10, i -> i+1)    <strong>// with three arguments</strong>
            <strong>.collect</strong>(Collectors.toList());
      System.out.println("In Java 9:" + numbers2);
   }
}

输出

<strong>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]</strong>

以上就是Stream API的iterate()方法在Java 9中的重要性是什么?的详细内容,更多请关注其它相关文章!