1import java.util.stream.Stream;
2
3class Example {
4
5 public static void main(String[] args) {
6 int len = 5; // For example.
7
8 // Use Stream to initialize array.
9 Foo[] arr = Stream.generate(() -> new Foo(1)) // Lambda can be anything that returns Foo.
10 .limit(len)
11 .toArray(Foo[]::new);
12 }
13
14 // For example.
15 class Foo {
16 public int bar;
17
18 public Foo(int bar) {
19 this.bar = bar;
20 }
21 }
22}
23