2

Given a simple generic class:

private static class Container<T> {
    private List<T> aList;
    private T aValue;

    private Container(List<T> aList, T aValue) {
        this.aList = aList;
        this.aValue = aValue;
    }
}

Initialize a list of that class:

List<Container<?>> container = new ArrayList<>();
// Add some elements...

Not possible (The method toArray(IntFunction<A[]>) in the type Stream<List<capture#1-of ?>> is not applicable for the arguments (List<?>[])):

container.stream().map(value -> value.aList).toArray(new List<?>[0]);

Possible:

container.stream().map(value -> value.aList).collect(Collectors.toList()).toArray(new List<?>[0]);

Why?

0

1 Answer 1

1

Stream's toArray takes a IntFunction<A[]> - i.e. a function that accepts an int and returns an array.

You tried to pass an array to it.

It should be used as follows:

container.stream().map(value -> value.aList).toArray(List<?>[]::new)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.