Java List parameterized?

2019-06-26 01:39发布

问题:

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 <DLPFile >fileList =  new ArrayList <DLPFile>();
fileList.add(file);
session.put("filesList", fileList);

but how do I retrieve the list from the session var? When I do:

List <DLPFile files = (List) session.get("fileslist");

I got some warnings:

"List is a raw type.References to generic type List<E> should be parameterized."

I tried

List <DLPFile files = (List <DLPFile> ) session.get("fileslist");   
List <DLPFile files = (List ) session.get("fileslist")<DLPFile>; and
List <DLPFile files = (List) <DLPFile>  session.get("fileslist");

but none works

I suppose this is kind of a "casting" problem... (maybe?)

Thanks in advance ;)

回答1:

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<T> 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<Object> 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.



回答2:

Are you aware that you are missing a > at the start? i.e. 'List <DLPFile files' should be 'List <DLPFile>' files'.



回答3:

This option should be fine

List <DLPFile> files = (List <DLPFile>) session.get("fileslist"); 

Although you'll get an unchecked cast warning since I don't expect your session to be a Map<String, List<DLPFile>>.

Look through the Java Generics FAQ http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html



回答4:

The first option you tried is the one you need:

List<DLPFile> files = (List<DLPFile>) session.get("fileslist");

The first warning you get doesn't have anything to do with generics: You would get the warning when casting to, for example, String as well. The compiler just tells you it can't ensure the returned object is a List<DLPFile>.

The second one, about the raw type, does have to do with generics. If you use the option above, you shouldn't get it, but if you cast to just List, you will get it. It tells you you shouldn't use List without the type parameter, in your case <DLPFile>.



回答5:

As others have said, in Java you can't safely cast generic types.

Rather than ignoring the compiler, a better way to fix this problem is to put something a bit more meaningful into the session. Wrap the List in a class that handles this sort of collection of the type. Heck, even add a few instance methods that do something meaningful with the list, rather than a getFiles method.