I am quite new to Java ...
I wrote a class called DLPFile
which is basically a container of other objects like Strings, ints, floats, etc.
When putting my files into a List and then saving it in my session (which is from Map class) variable is easy;
DLPFile file = new DLPFile();
List fileList = new ArrayList ();
fileList.add(file);
session.put("filesList", fileList);
but how do I retrieve the list from the session var?
When I do:
List
I got some warnings:
"List is a raw type.References to generic type List should be parameterized."
I tried
List ) session.get("fileslist");
List ; and
List session.get("fileslist");
but none works
I suppose this is kind of a "casting" problem... (maybe?)
Thanks in advance ;)
解决方案
This is because of the Generics Type erasure. The compiler has no way to determine the actual generic type argument when you get it from your session (except if you session.get takes a Class argument to cast it accordingly), because the session probably only returns the type of object. You can make sure that your object is an instance of List, but the generic type information is lost (basically the compiler converts it to a List internally). That is why you get the warning, because only you as the programmer can know if the generic type parameter you want to cast it to is the right one.
If you don't like the warning you may add an
@SuppressWarnings("unchecked")
Annotation at the beginning of your method.