Reputation: 2761
What if I want to have a variable, say a List, which can be instantiated with any Type?
So, given:
List list;
I could do any of these:
list = new ArrayList<String>();
list = new Vector<Integer>();
list = new LinkedList<HashMap>();
Edit: Alternatively, I may be using a library that returns a raw List. For example, in Hibernate:
List result = session.createQuery( "from Event" ).list();
Eclipse gives me this warning when I want to use List list
:
List is a raw type. References to generic type
List<E>
should be parameterized
Why should this be avoided?
Upvotes: 1
Views: 2071
Reputation: 301
try this one it will accept anything which extends Object into your list.
List<? extends Object> list;
list = new ArrayList<String>();
list = new Vector<Integer>();
list = new LinkedList<Map>();
Upvotes: 0
Reputation: 4113
You are getting this warning because you have parametrized the arraylist only and not the list object. So a mixture of generic and non-generic. This can lead to runtime casting exceptions, failing the purpose of generic.
List<String> list = new ArrayList<>(); //If JDK7, not back comptible
List<String> list = new ArrayList<String>(); //If <=JDK7, back compatible
e.g.
List<Integer> list1 = new ArrayList<>();
List list2 = new ArrayList<Integer>();
Just when you try to do an add function of both the lists, you can see the difference,
list1.add("sdfsdfd"); //Takes only integer inputs, compilation error
list2.add(obj); //Takes object type, since obj is not integer type, so will throw classcastexception
Upvotes: 0
Reputation: 612
Below code should be fine:
List<
or?
> list;
List<
?
extends Object> list;
For usage of question mark(?) in java, you can refer to Wildcard (Java).
Upvotes: -1
Reputation: 135772
Eclipse gives me this error when I want to do the above:
I believe eclipse gives you a warning (yellow underlining), not an error (red underlining).
List is a raw type. References to generic type List should be parameterized
Why should this be avoided?
References to generic types should be parameterized because they provide stronger typing. This will help you detect at compile time some problems that otherwise would only be detected at execution time.
For instance:
List myIntegerList = new ArrayList();
myIntegerList.add(1);
myIntegerList.add("bad bad String");
Will compile without problems; but will give you an error at execution time. On the other side:
List<Integer> myIntegerList = new ArrayList<Integer>();
myIntegerList.add(1);
myIntegerList.add("bad bad String"); // compiler will show an error here
Will never compile, as it will show a clear error when trying to add a String
to a Integer
's List
.
Upvotes: 5
Reputation: 7899
Just because you can do something does not mean it is wise to do. Under what circumstances would you have an object that could be an ArrayList<String>
, a Vector<Integer>
and a LinkedList<HashMap>
at different times? Objects of different types have different purposes. You would probably want a class with a List<String>
instance variable, a List<Integer>
instance variable, and a List<Map<K, V>>
instance variable. Then the problem goes away.
Doing what you suggest will be a great and unpleasant surprise to your coworkers. Keep them happy; do not annoy them to demonstrate your good memory. And, six months from now, do you expect to understand the code you wrote today? Google or look up on this site IOCCC. Those programs were written to be clever, not to be paradigms of best practices.
Upvotes: 1
Reputation: 28752
It is not an error according to Java language, your eclipse is probably configured to flag it as an error. Now, can you identify a usecase where you would want a same variable to hold sometimes Strings
and sometimes Integer
? As to why this error, anything you get out of it has to be downcasted to the correct type, with the risk of runtime error:
List list = new ArrayList<Integer>();
list.add("string"); // no error from compiler
Map m = (Map) list.get(0); // no error from compiler, but runtime error
compare this to
List<Integer> list = new ArrayList<Integer>();
list.add("string"); // compile error
Map m = (Map) list.get(0); // compile error
Upvotes: 2