1

Can I create array of suppliers? This doesn't compile somewhy:

Supplier<String>[] x = new Supplier<String>[] {
    () -> "f"
};
Michael
  • 34,340
  • 9
  • 58
  • 100
user_x
  • 657
  • 1
  • 7
  • 16
  • 2
    Better yet, use a List. Generics and arrays don't mix well because of the different approaches to variance – Michael May 11 '21 at 15:05
  • ...or if you could explain what restricts you to use an array instead of a `List`? – Naman May 11 '21 at 15:07
  • @Michael: The linked duplicate is really **not relevant** to this question. The OP asked for the array -> you suggested a List -> you linked a duplicate about creating an array of Lists. If the question should be closed as a duplicate, please refer to a more relevant existing question such as a generic array or array of lambda expressions creation. – Nikolas Charalambidis May 12 '21 at 09:26
  • @NikolasCharalambidis No. It's an exact duplicate. The problem is how to create a `Foo[]`. Whether that's `Supplier[]` or `ArrayList[]`, it doesn't matter. The problem is the same. And we don't need a question for every single built-in type which takes a single generic type parameter – Michael May 12 '21 at 09:28
  • @NikolasCharalambidis "you suggested a List -> you linked a duplicate about creating an array of Lists" You list these things as causational, but they are not. You can consider them unrelated. My advice is to use a List. But if they want to ignore my advice, then the linked duplicate has plenty of other suggestions. – Michael May 12 '21 at 09:32
  • @Michael "No. It's an exact duplicate." - Linking an exact duplicate would eventually lead the OP to the better solution since answers in questions about generics used in the array always suggest using a List instead. Moreover, you really don't know if the OP is not limited by either internal design or external API. The linked duplicate is incorrect, IMHO. – Nikolas Charalambidis May 12 '21 at 09:48
  • @NikolasCharalambidis "*since answers in questions about generics used in the array **always** suggest using a List*" Here are [one](https://stackoverflow.com/a/28588120/1898563), [two](https://stackoverflow.com/a/8559232/1898563), [three](https://stackoverflow.com/a/17512961/1898563) answers in the linked duplicate that directly contradict that statement. "*Moreover, you really don't know if the OP is not limited by either internal design or external API.*" Irrelevant. The linked duplicate shows solutions involving lists and arrays, so all cases are covered. – Michael May 12 '21 at 10:24

2 Answers2

3

You have to create an array of raw Supplier:

Supplier<String>[] x = new Supplier[] {
    () -> "f"
};

It's not possible to instantiate a generic array.

Eran
  • 359,724
  • 45
  • 626
  • 694
2

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");
WJS
  • 22,083
  • 3
  • 14
  • 32