Java列表参数化?

发布于 2024-08-03 08:14:05 字数 880 浏览 4 评论 0 原文

我对 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");

,但没有任何效果

我想这是一种“铸造”问题......(也许?)

提前致谢;)

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 ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

明月夜 2024-08-10 08:14:05

这是因为泛型类型被删除了。当您从会话中获取实际的泛型类型参数时,编译器无法确定它(除非您 session.get 采用 Class 参数来相应地转换它),因为会话可能只返回对象的类型。您可以确保您的对象是 List 的实例,但通用类型信息会丢失(基本上编译器在内部将其转换为 List)。这就是您收到警告的原因,因为只有您作为程序员才能知道您想要将其转换为的泛型类型参数是否正确。

如果您不喜欢这个警告,您可以

@SuppressWarnings("unchecked")

在方法的开头添加注释。

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.

做个少女永远怀春 2024-08-10 08:14:05

您是否知道您错过了一个 >一开始?即“列表 ”文件'。

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

网名女生简单气质 2024-08-10 08:14:05

此选项应该没问题

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

,但您会收到未经检查的强制转换警告,因为我不希望您的会话是 Map>

查看 Java 泛型常见问题解答 http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html< /a>

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

深白境迁sunset 2024-08-10 08:14:05

您尝试的第一个选项就是您需要的选项:

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

您收到的第一个警告与泛型没有任何关系:例如,在转换为 String 时您也会收到警告。编译器只是告诉您它无法确保返回的对象是 List

第二个是关于原始类型的,确实与泛型有关。如果您使用上面的选项,您不应该得到它,但如果您仅转换为 List,您就会得到它。它告诉您不应在没有类型参数的情况下使用 List,在您的情况下为

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

叹梦 2024-08-10 08:14:05

正如其他人所说,在 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 a getFiles method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文