Reputation: 2164
A method of some poorly documented library is returning me a List of Objects. I know that the list contains ArrayLists of integers. As follows:
List<?> result = lib.get();
Iterator<?> iterator = result.iterator();
while(iterator.hasNext()){
Object next = iterator.next();
System.out.println(next.toString());
System.out.println(next.getClass());
}
Gets me
[0, 0]
class java.util.Arrays$ArrayList
[0, 1]
class java.util.Arrays$ArrayList
[0, 2]
class java.util.Arrays$ArrayList
How can I get to turn those objects into ArrayList so that I can use them properly?
Upvotes: 2
Views: 2171
Reputation: 159086
Just cast the value returned by lib.get()
to List<List<Integer>>
.
@SuppressWarnings("unchecked")
List<List<Integer>> result = (List<List<Integer>>) lib.get();
Proof
// Simulate lib.get() and prove that it is same as question code
List<?> result = Arrays.asList(Arrays.asList(0, 0),
Arrays.asList(0, 1),
Arrays.asList(0, 2));
for (Object next : result) {
System.out.println(next);
System.out.println(next.getClass());
}
// Fix it
@SuppressWarnings("unchecked")
List<List<Integer>> fixedResult = (List<List<Integer>>) result;
// Print the fixed result
for (List<Integer> list : fixedResult) {
for (int value : list)
System.out.print(value + " ");
System.out.println();
}
Output
[0, 0]
class java.util.Arrays$ArrayList
[0, 1]
class java.util.Arrays$ArrayList
[0, 2]
class java.util.Arrays$ArrayList
0 0
0 1
0 2
Upvotes: 0
Reputation: 983
I think you can only cast the Object next = ... object into the type java.util.Arrays$ArrayList, but because it is a private static class, it is better to cast it to java.util.List class.
Like:
List<?> result = lib.get();
Iterator<?> iterator = result.iterator();
while(iterator.hasNext()){
Object next = iterator.next();
// -------------------
List<Integer> list = (List<Integer>) next;
System.out.println(list.get(0));
System.out.println(list.get(1));
// -------------------
}
Upvotes: 1
Reputation: 338406
You cannot cast the entire thing, as it is a List
of List
objects that hold Integer
objects (apparently). So loop the outer list, and cast each element List
object to be a List< Integer >
object.
You appear to have a list of lists of integers, not a list of integers.
List < Object > objects = new ArrayList <> ( 3 );
objects.add ( List.of ( 0 , 0 ) );
objects.add ( List.of ( 0 , 1 ) );
objects.add ( List.of ( 0 , 2 ) );
System.out.println ( objects );
[[0, 0], [0, 1], [0, 2]]
Unfortunately, you cannot simply cast to a concrete type as suggested in the other Answer because we do not know the concrete type. We can presume that we have a List
, but not a ArrayList
.
See this example. The List.of
method generates an object of an unspecified class that implements List
. We can see here that whatever concrete class is used, it is not ArrayList
.
List < Object > objects = new ArrayList <> ( 3 );
objects.add ( List.of ( 0 , 0 ) );
objects.add ( List.of ( 0 , 1 ) );
objects.add ( List.of ( 0 , 2 ) );
System.out.println ( objects );
Iterator < ? > iterator = objects.iterator ();
while ( iterator.hasNext () )
{
Object next = iterator.next ();
List < Integer > list = ( ArrayList < Integer > ) next;
System.out.println ( list.get ( 0 ) );
System.out.println ( list.get ( 1 ) );
}
…throws an exception:
Exception in thread "main" java.lang.ClassCastException: class java.util.ImmutableCollections$List12 cannot be cast to class java.util.ArrayList (java.util.ImmutableCollections$List12 and java.util.ArrayList are in module java.base of loader 'bootstrap')
We can cast each element to a List< Integer >
.
List < Object > objects = new ArrayList <> ( 3 );
objects.add ( List.of ( 0 , 0 ) );
objects.add ( List.of ( 0 , 1 ) );
objects.add ( List.of ( 0 , 2 ) );
System.out.println ( objects );
Iterator < ? > iterator = objects.iterator ();
while ( iterator.hasNext () )
{
Object next = iterator.next ();
List < Integer > list = ( List < Integer > ) next;
System.out.println ( list.get ( 0 ) );
System.out.println ( list.get ( 1 ) );
}
When run.
[[0, 0], [0, 1], [0, 2]]
0
0
0
1
0
2
I do not know of a way to directly cast the original list. This next line gives a compiler error of inconvertible types
.
List < List < Integer > > listOfListOfIntegers = ( List < List < Integer > > ) objects;
Upvotes: 0
Reputation: 5695
Assuming you have a list of lists of integers, you can simply do
List<List<Integer>> newList = result.stream()
.map(e->(List<Integer>)e)
.collect(Collectors.toList());
Upvotes: 0