you can use
String[] array = { "a", "b", "c" };
Stream.generate(() -> array).flatMap(Arrays::stream).forEach(System.out::println);
for printing a b cforever and
String[] array = { "a", "b", "c" };
Stream.generate(() -> array).limit(2).flatMap(Arrays::stream).forEach(System.out::println);
print a b ctwice.
It does not even require an existing array:
Stream.generate(() -> null)
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);
respectively.
Stream.generate(() -> null).limit(2)
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);
you can also use
IntStream.range(0, 2).boxed()
.flatMap(x -> Stream.of("a", "b", "c"))
.forEach(System.out::println);
source
share