Vemu
Vemu

Reputation: 340

How to convert List<List<String>> into List<List<Object>>?

Like in convert how to convert List<List<String>> into List<List<Object>>? For example getValue() returns List<List<String>> and then I need to pass this List to Adapter's constructor as List<List<Object>>. I don't to generify this constuctor. How can I do that? When I try to cast I get Inconvertible types error

new TableTimetableAdapter(Repo.getValue());

Upvotes: 0

Views: 87

Answers (3)

Miguel Hern&#225;ndez
Miguel Hern&#225;ndez

Reputation: 55

I think you can't just cast a List of a type to another type, you would need to map the content of the lists into the desired sub-type.

You could do something like this:

List<List<Object>> objects = Repo.getValue().stream()
        .map(strings -> strings.stream().map(string -> (Object) string).collect(Collectors.toUnmodifiableList()))
        .collect(Collectors.toUnmodifiableList());

Upvotes: 0

tastaturtier
tastaturtier

Reputation: 126

Since List<List<String>> is more specific than List<List<Object>> you do not need to cast.

Upvotes: 0

Alex R
Alex R

Reputation: 3311

Casting should actually work... You are not allowed to cast List<List<String>> to List<List<Object>> but you can just cast it it to List like so:

new TableTimetableAdapter((List) Repo.getValue());

Upvotes: 3

Related Questions