0

I use following code to create an array of List<String>, but the compiler complains generic array creation:

List<String>[] array = new List<String>[10];

Also, I would like to convert a list of List<String>, the compiler also comlains generic array creation with the following code:

        List<List<String>> results = new ArrayList<>();
        results.stream().toArray(List<String>[]::new);

I would like to ask how to create an array of List<String>, and how to convert List<List<String>> to an array of List<String>

Tom
  • 5,848
  • 12
  • 44
  • 104
  • 1
    Does this answer your question? [How to create a generic array in Java?](https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Ricky Mo Jun 26 '23 at 03:46
  • 2
    Why? Why not a `List>`? More, err, generic that way. – user207421 Jun 26 '23 at 03:51
  • Does this answer your question? [How can I create an Array of ArrayLists?](https://stackoverflow.com/questions/8559092/how-can-i-create-an-array-of-arraylists) – Ahmed Nabil Jun 26 '23 at 04:08
  • Can you give some example, what you mean by converting List> to List? – Gaurav Jeswani Jun 26 '23 at 04:09
  • @GauravJeswani I would like to convert List> to an array of List, this are two cases to demonstrate my question. – Tom Jun 26 '23 at 04:20
  • @Tom If you _really_ need to, you can do: `List>[] arr = results.stream().toArray(List>[]::new)`, cast the result to `List[]` and suppress the warning. – Alex R Jun 26 '23 at 04:24
  • Sounds like an [XY Problem](https://xyproblem.info/) – shmosel Jun 26 '23 at 04:37
  • Does this help? [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array) – Abra Jun 26 '23 at 04:38

3 Answers3

0

The short answer is you can't, Java arrays don't support generic types.

There are very few cases where you can't use a List<List<String>> instead of an array of List<String>. Try that first and come back if you can't do that.

Basically, your choices are make an array of Object, and cast the elements to (List<String>) when you access them, or make an array of List, and again cast to (List<String>) if you need to.

Or make a wrapper around the array that will do the casting for you. Which is what a List<List<String>> is, so you're back to that question - why not do that?

John Bayko
  • 746
  • 4
  • 7
  • Yes, in practice, i would use List>, I just come across such a problem, and would like to know how to do the conversion. – Tom Jun 26 '23 at 04:23
0

Array of generic type are not supported in java. Instead if you create an array like

List[] array = new List[10]; Than it will work.

Please refer the below question for more details:

https://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java#:~:text=It's%20because%20Java's%20arrays%20

Sachin Mewar
  • 503
  • 5
  • 6
0

Try it like this. Just omit the generic type in the toArray method. The suppress warnings removes the compiler's type safety warning (you may get a runtime class cast exception if the resulting types don't match since the types are erased). But in this case, it isn't a problem.

List<List<String>> results = new ArrayList<>(
        List.of(List.of("ABC", "DEF"), List.of("GHI", "JKL")));

@SuppressWarnings("unchecked")
List<String>[] array = results.stream().toArray(List[]::new);
for (List<String> lst : array) {
    System.out.println(lst);
}

String a = array[0].get(1);
System.out.println(a);

prints

[ABC, DEF]
[GHI, JKL]
DEF

Contrast the following using the same data and see what happens when you try to get an "Integer" from a list in the array.

@SuppressWarnings("unchecked")
List<Integer>[] array = results.stream().toArray(List[]::new);
Integer a = array[0].get(1);  // class cast exception!!
System.out.println(a);

And finally, you can also do it sans streams.

@SuppressWarnings("unchecked")
List<String>[] arr = new ArrayList[2];
arr[0] = new ArrayList<>(List.of("ABC","DEF"));
arr[1] = new ArrayList<>(List.of("GHI","JKL"));

String a = arr[1].get(0);
System.out.println(a);

prints

GHI
WJS
  • 36,363
  • 4
  • 24
  • 39