Can I create array of suppliers? This doesn't compile somewhy:
Supplier<String>[] x = new Supplier<String>[] {
() -> "f"
};
Can I create array of suppliers? This doesn't compile somewhy:
Supplier<String>[] x = new Supplier<String>[] {
() -> "f"
};
You have to create an array of raw Supplier
:
Supplier<String>[] x = new Supplier[] {
() -> "f"
};
It's not possible to instantiate a generic array.
You can do it like this. But it would be best to use a List.
@SuppressWarnings("unchecked")
Supplier<String>[] sups = new Supplier[]{()->"A", ()->"B", ()->"C"};
for (Supplier<String> s : sups) {
System.out.println(s.get());
}
prints
A
B
C
This would be my preferred way of doing it. The List returned by List.of
will be immutable.
List<Supplier<String>> sups = List.of(()->"A", ()->"B", ()->"C");