Java 如何解决“List 类型的表达式需要未经检查的转换...”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:53:02  来源:igfitidea点击:

How do I fix "The expression of type List needs unchecked conversion...'?

javawarningsunchecked-conversion

提问by user46277

In the Java snippet:

在 Java 代码段中:

SyndFeedInput fr = new SyndFeedInput();
SyndFeed sf = fr.build(new XmlReader(myInputStream));
List<SyndEntry> entries = sf.getEntries();

the last line generates the warning

最后一行生成警告

"The expression of type Listneeds unchecked conversion to conform to List<SyndEntry>"

“类型的表达式List需要未经检查的转换才能符合List<SyndEntry>

What's an appropriate way to fix this?

什么是解决此问题的适当方法?

采纳答案by erickson

Since getEntriesreturns a raw List, it could hold anything.

由于getEntries返回一个 raw List,它可以容纳任何东西。

The warning-free approach is to create a new List<SyndEntry>, then cast each element of the sf.getEntries()result to SyndEntrybefore adding it to your new list. Collections.checkedListdoes notdo this checking for you—although it would have been possible to implement it to do so.

无警告方法是创建一个 new List<SyndEntry>,然后将sf.getEntries()结果的每个元素转换为 ,然后再将其SyndEntry添加到新列表中。Collections.checkedList没有做这个检查你-虽然它本来可以实现它这样做。

By doing your own cast up front, you're "complying with the warranty terms" of Java generics: if a ClassCastExceptionis raised, it will be associated with a cast in the source code, not an invisible cast inserted by the compiler.

通过ClassCastException预先进行自己的转换,您“遵守了 Java 泛型的保证条款”:如果 a被引发,它将与源代码中的转换相关联,而不是编译器插入的不可见转换。

回答by Alex B

Did you write the SyndFeed?

你写的SyndFeed

Does sf.getEntriesreturn List or List<SyndEntry>? My guess is it returns Listand changing it to return List<SyndEntry>will fix the problem.

是否sf.getEntries返回列表或List<SyndEntry>?我的猜测是它返回List并将其更改为返回List<SyndEntry>将解决问题。

If SyndFeedis part of a library, I don't think you can remove the warning without adding the @SuppressWarning("unchecked")annotation to your method.

如果SyndFeed是库的一部分,我认为您不能在不向@SuppressWarning("unchecked")方法中添加注释的情况下删除警告。

回答by Shyam

If you look at the javadoc for the class SyndFeed(I guess you are referring to the class com.sun.syndication.feed.synd.SyndFeed), the method getEntries() doesn't return java.util.List<SyndEntry>, but returns just java.util.List.

如果您查看该类的 javadoc SyndFeed(我猜您指的是该类com.sun.syndication.feed.synd.SyndFeed),方法 getEntries() 不会返回java.util.List<SyndEntry>,而只返回java.util.List.

So you need an explicit cast for this.

所以你需要一个明确的演员阵容。

回答by Jon Skeet

It looks like SyndFeedis not using generics.

看起来SyndFeed没有使用泛型。

You could either have an unsafe cast and a warning suppression:

你可以有一个不安全的演员和一个警告抑制:

@SuppressWarnings("unchecked")
List<SyndEntry> entries = (List<SyndEntry>) sf.getEntries();

or call Collections.checkedList- although you'll still need to suppress the warning:

或调用Collections.checkedList- 尽管您仍然需要取消警告:

@SuppressWarnings("unchecked")
List<SyndEntry> entries = Collections.checkedList(sf.getEntries(), SyndEntry.class);

回答by Boune

If you don't want to put @SuppressWarning("unchecked") on each sf.getEntries() call, you can always make a wrapper that will return List.

如果您不想在每个 sf.getEntries() 调用上放置 @SuppressWarning("unchecked"),您始终可以制作一个将返回 List 的包装器。

See this other question

看到另一个问题

回答by Bruno De Fraine

This is a common problem when dealing with pre-Java 5 APIs. To automate the solution from erickson, you can create the following generic method:

这是处理 Java 5 之前的 API 时的常见问题。要从erickson自动化解决方案,您可以创建以下通用方法:

public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
    List<T> r = new ArrayList<T>(c.size());
    for(Object o: c)
      r.add(clazz.cast(o));
    return r;
}

This allows you to do:

这允许您执行以下操作:

List<SyndEntry> entries = castList(SyndEntry.class, sf.getEntries());

Because this solution checks that the elements indeed have the correct element type by means of a cast, it is safe, and does not require SuppressWarnings.

因为此解决方案通过强制转换检查元素确实具有正确的元素类型,所以它是安全的,并且不需要SuppressWarnings.

回答by DennisTemper

Even easier

更轻松

return new ArrayList<?>(getResultOfHibernateCallback(...))

return new ArrayList<?>(getResultOfHibernateCallback(...))

回答by Joseph K. Strauss

If you are using Guava and all you want to do is iterate through your values:

如果您正在使用 Guava 并且您想要做的就是遍历您的值:

for(SyndEntry entry: Iterables.filter(sf.getEntries(), SyndEntry.class){
  ...
}

If you need an actual List you can use

如果你需要一个实际的列表,你可以使用

List<SyndEntry> list = Lists.newArrayList(
    Iterables.filter(sf.getEntries(), SyndEntry.class));

or

或者

List<SyndEntry> list = ImmutableList.copyOf(
    Iterables.filter(sf.getEntries(), SyndEntry.class));

回答by Honglonglong

SyndFeedInput fr = new SyndFeedInput();
SyndFeed sf = fr.build(new XmlReader(myInputStream));
List<?> entries = sf.getEntries();