Java列表参数化?
我对 Java 很陌生...... 我写了一个名为 DLPFile 的类 它基本上是其他对象的容器,如字符串、整数、浮点数等。
当将我的文件放入列表中,然后将其保存在我的会话(来自 Map 类)中时,变量很容易;
DLPFile file = new DLPFile();
List <DLPFile >fileList = new ArrayList <DLPFile>();
fileList.add(file);
session.put("filesList", fileList);
但如何从会话变量中检索列表? 当我这样做时:
List <DLPFile files = (List) session.get("fileslist");
我收到一些警告:
"List is a raw type.References to generic type List<E> should be parameterized."
我尝试过
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");
,但没有任何效果
我想这是一种“铸造”问题......(也许?)
提前致谢;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为泛型类型被删除了。当您从会话中获取实际的泛型类型参数时,编译器无法确定它(除非您 session.get 采用 Class 参数来相应地转换它),因为会话可能只返回对象的类型。您可以确保您的对象是 List 的实例,但通用类型信息会丢失(基本上编译器在内部将其转换为 List
如果您不喜欢这个警告,您可以
在方法的开头添加注释。
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
Annotation at the beginning of your method.
您是否知道您错过了一个 >一开始?即“列表”文件'。
Are you aware that you are missing a > at the start? i.e. 'List <DLPFile files' should be 'List <DLPFile>' files'.
此选项应该没问题
,但您会收到未经检查的强制转换警告,因为我不希望您的会话是
Map>
。查看 Java 泛型常见问题解答 http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html< /a>
This option should be fine
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
您尝试的第一个选项就是您需要的选项:
您收到的第一个警告与泛型没有任何关系:例如,在转换为
String
时您也会收到警告。编译器只是告诉您它无法确保返回的对象是List
。第二个是关于原始类型的,确实与泛型有关。如果您使用上面的选项,您不应该得到它,但如果您仅转换为
List
,您就会得到它。它告诉您不应在没有类型参数的情况下使用List
,在您的情况下为
。The first option you tried is the one you need:
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 aList<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 useList
without the type parameter, in your case<DLPFile>
.正如其他人所说,在 Java 中你不能安全地转换泛型类型。
解决此问题的更好方法是在会话中添加一些更有意义的内容,而不是忽略编译器。将
List
包装在处理此类类型集合的类中。哎呀,甚至添加一些对列表执行有意义的操作的实例方法,而不是getFiles
方法。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 agetFiles
method.