你可以用
String[] array = { "a", "b", "c" };
Stream.generate(() -> array).flatMap(Arrays::stream).forEach(System.out::println);
永远打印一个b c
String[] array = { "a", "b", "c" };
Stream.generate(() -> array).limit(2).flatMap(Arrays::stream).forEach(System.out::println);
打印一次b c两次.
这甚至不需要现有的数组:
Stream.generate(() -> null)
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);
RESP.
Stream.generate(() -> null).limit(2)
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);
你也可以用
IntStream.range(0, 2).boxed()
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);