0

I came up with below solution, not sure if Type:safety warnings can be removed using something else.

BiFunction<List<List<T>>, Class<T>,T[][]> toArray = (list,type) ->
{
    T a[][] = (T[][]) Array.newInstance(type,
            list.size(), list.get(0).size());
    IntStream.range(0, a.length)
    .forEach(i -> {
        a[i]=(T[]) list.get(i).toArray();
    });
    return a;
};

Also, if this could be improved with one single pipeline I would appreciate the solution.

4
  • List.toArray() without an argument will always return Object[] - you'll need to create proper instances for the components as well, not just for the larger array. Where do you get your warnings? Commented Apr 8, 2019 at 17:44
  • sorry, but I don't understand what you want to do exactly? convert List<List<T>> to T[][] Commented Apr 8, 2019 at 17:44
  • I get the warnings on the first assignment, also I'd like to say that I had to rewrite the method when I tested this on Integer matrix , so now I use below BiFunction<List<List<T>>, Class<T>,T[][]> toArray = (list,type) -> { T a[][] = (T[][]) Array.newInstance(list.get(0).get(0).getClass(), list.size(), list.get(0).size()); IntStream.range(0, list.size()) .forEach(i -> { IntStream.range(0, list.get(0).size()) .forEach(j -> { a[i][j]=list.get(i).get(j); }); }); return a; }; Commented Apr 8, 2019 at 20:38
  • YCF_L yes that's what I'm looking for, fyi, the code works but I'd like a more elegant way of doing this using streams and maps, and don't have to use the forEach statement which cannot be run on parallel stream Commented Apr 8, 2019 at 20:40

1 Answer 1

1

I have two suggestions, in both, convert the from List to List iterating List of List and adding each element as T[] in a new list.

Use Array.newInstance only for create auxiliar objects and add @SuppressWarnings annotation

    @SuppressWarnings("unchecked")
    public static <T> T[][] toArray(List<List<T>> list, Class<T> type) {

    T aux[] = (T[]) Array.newInstance(type, 0);
    T auxBi[][] = (T[][]) Array.newInstance(type, 0, 0);

    List<T[]> newList = new ArrayList<>();
    list.forEach(e -> newList.add(e.toArray(aux)));

    T[][] newBi = newList.toArray(auxBi);

    return newBi;
}

or

add auxliars as parameters instead of type

public static <T> T[][] toArray(List<List<T>> list, T aux[], T auxBi[][]) {

    List<T[]> newList = new ArrayList<>();
    list.forEach(e -> newList.add(e.toArray(aux)));

    T[][] newBi = newList.toArray(auxBi);

    return newBi;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Appreciate this answer, this gave me an idea of what I was looking to achieve.
Solution above using a stream pipeline public BiFunction<List<List<T>>,Class<T>, T[][]> toArr = (list,type)-> list.stream().map(row -> row.toArray((T[]) Array.newInstance(type, 0))).collect(Collectors.toList()).toArray((T[][]) Array.newInstance(type, 0, 0));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.