I have two questions, actaully... First off, Why cant I do this:
List<Object> object = new List<Object>();
And second, I have a method that returns a List<?>
, how would I turn that into a List<Object>
, would I be able to simply cast it?
Thank you!
package com.test;
import java.util.ArrayList;
import java.util.List;
public class TEst {
public static void main(String[] args) {
List<Integer> ls=new ArrayList<>();
ls.add(1);
ls.add(2);
List<Integer> ls1=new ArrayList<>();
ls1.add(3);
ls1.add(4);
List<List<Integer>> ls2=new ArrayList<>();
ls2.add(ls);
ls2.add(ls1);
List<List<List<Integer>>> ls3=new ArrayList<>();
ls3.add(ls2);
m1(ls3);
}
private static void m1(List ls3) {
for(Object ls4:ls3)
{
if(ls4 instanceof List)
{
m1((List)ls4);
}else {
System.out.print(ls4);
}
}
}
}
List is an interface so you can't instanciate it. Use any of its implementatons instead e.g.
List<Object> object = new List<Object>();
About List : you can use any object as a generic param for it instance:
List<?> list = new ArrayList<String>();
or
List<?> list = new ArrayList<Integer>();
While using List<Object>
this declaration is invalid because it will be type missmatch.
List<Object> object = new List<Object>();
You cannot do this because List is an interface and you cannot create object of any interface or in other word you cannot instantiate any interface. Moreover, you can assign any object of class which implements List to its reference variable. For example you can do this:
list<Object> object = new ArrayList<Object>();
Here ArrayList is a class which implements List, you can use any class which implements List.
To answer your second question, yes, you can cast the List<?>
as a List<Object>
or a List<T>
of any type, since the ?
(Wildcard) parameter indicates that the list contains a homogenous collection of an any Object
. However, there's no way to know at compile what the type
is since it's part of the exported API only - meaning you can't see what's being inserted into the List<?>
.
Here's how you would make the cast:
List<?> wildcardList = methodThatReturnsWildcardList();
// generates Unchecked cast compiler warning
List<Object> objectReference = (List<Object>)wildcardList;
In this case you can ignore the warning because in order for an object to be used in a generic class it must be a subtype of Object
. Let's pretend that we're trying to cast this as a List<Integer>
when it actually contains a collection of String
s.
// this code will compile safely
List<?> wildcardList = methodThatReturnsWildcardList();
List<Integer> integerReference = (List<Integer>)wildcardList;
// this line will throw an invalid cast exception for any type other than Integer
Integer myInteger = integerRefence.get(0);
Remember: generic types are erased at runtime. You won't know what the collection contains, but you can get an element and call .getClass()
on it to determine its type.
Class objectClass = wildcardList.get(0).getClass();
Source: Stackoverflow.com