Java “Iterable<Element> 不能转换为 List<Element>” - `List` 不是一种 `Iterable` 类型吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3959892/
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-14 07:36:41  来源:igfitidea点击:

"Iterable<Element> cannot be cast to List<Element>" - Isn't `List` a type of `Iterable`?

javalistclasscastexceptioniterable

提问by snoopy

I called a getElementsmethod which returns Iterable<Element>.

我调用了一个getElements返回Iterable<Element>.

I did this:

我这样做了:

List<Element> elements = (List<Element>) getElements();

This generates the error:

这会产生错误:

java.lang.ClassCastException: com.utesy.Element 
cannot be cast to java.util.List

I thought a Listwas a type of Iterable?

我以为 aList是一种Iterable?

采纳答案by Jon Skeet

Yes, List<T>extends Iterable<T>, but that doesn't mean that you can cast from anyIterable<T>to List<T>- only when the value actuallyrefers to an instance of a type of List<T>. It's entirely possible to implement Iterable<T>without implementing the rest of the List<T>interface... in that case, what would you expect to happen?

是的,List<T>extends Iterable<T>,但这并不意味着您可以从任何转换Iterable<T>List<T>- 仅当该值实际上指的是List<T>. 完全有可能在Iterable<T>不实现List<T>接口的其余部分的情况下实现......在这种情况下,你希望发生什么?

To put it in simpler terms, let's change Iterable<T>to Objectand List<T>to String. Stringextends Object, so you can tryto cast from Objectto String... but the cast will only succeed at execution time if the reference actuallyrefers to a String(or is null).

说得简单一点,让我们改变Iterable<T>ObjectList<T>StringStringextends Object,因此您可以尝试Objectto String...进行强制转换,但只有在引用实际引用 a String(或为空)时,该转换才会在执行时成功。

回答by aioobe

Not all Iterables are Lists, thus it's not safe to cast an arbitrary Iterableto a List.

并非所有Iterables 都是Lists,因此将任意值强制转换Iterable为 a 是不安全的List

Take any Setfor instance, a HashSetis Iterablebut the elements has no order, so it can't implement the Listinterface, and is thus not a List.

以 anySet为例, aHashSetIterable但元素没有顺序,所以它不能实现List接口,因此不是 a List

回答by Jigar Joshi

From exception message it is clear that Iterable<Element>is not castable toList<Element>

从异常消息很明显, Iterable<Element>不能强制转换为List<Element>

SO you need to return List<Element>from getElements()

所以你需要List<Element>getElements()

回答by John Pickup

List extends Collection which in turn extends Iterable. You therefore trying to cast to a subtype which won't work unless getElements() really is returning a List (which the signature doesn't in any way guarantee).

List 扩展了 Collection,而 Collection 又扩展了 Iterable。因此,您尝试强制转换为一个子类型,除非 getElements() 确实返回一个 List(签名不能以任何方式保证),否则该子类型将不起作用。

See: http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html

请参阅:http: //download.oracle.com/javase/1.5.0/docs/api/java/util/List.html

回答by posdef

Listis a subinterfaceof Iterablemeaning that List includes pretty much everything that Iterable has, however not the other way around. So not all methods in a List instance would have an equivalent in Iterable.

List子接口Iterable意思是名单包括几乎一切的Iterable有,但不是相反。因此,并非 List 实例中的所有方法在 Iterable 中都具有等效项。

Try to avoid that sort of casting.

尽量避免这种类型的铸造。

I would recommend you to take a quick look at the Java 6 APIand the tutorials covering casting

我建议您快速浏览一下Java 6 API和涵盖强制转换的教程

回答by Erick Robertson

List<Element>is a type of Iterable<Element>, but that doesn't mean that all Iterable<Element>objects are List<Element>objects. You can cast a List<Element>as an Iterable<Element>, but not the other way around.

List<Element>是 的一种类型Iterable<Element>,但这并不意味着所有Iterable<Element>对象都是List<Element>对象。您可以将 a 转换List<Element>为 an Iterable<Element>,但不能反过来。

An apple is a type of fruit, but that doesn't mean that all fruits are apples. You can cast an apple as a fruit, but not the other way around.

苹果是一种水果,但这并不意味着所有的水果都是苹果。你可以把苹果当成水果,但反过来不行。

回答by Ash

List implements the Iterable interface but this doesn't mean Iterable can be cast back to List. Iterable is much more general and may be Hash or some exotic type that bears no relation to List. (It looks like getElements() returns an instance of some anonymous inner class contained alongside getElements within its class).

List 实现了 Iterable 接口,但这并不意味着 Iterable 可以转换回 List。Iterable 更通用,可能是 Hash 或一些与 List 无关的外来类型。(看起来 getElements() 返回包含在其类中的 getElements 旁边的某个匿名内部类的实例)。

If getElements would happen to contain Lists then this would be a valid cast. As the type returned by getElements() wasn't actually a List this produces a run-time error.

如果 getElements 碰巧包含列表,那么这将是一个有效的转换。由于 getElements() 返回的类型实际上不是 List,因此会产生运行时错误。

回答by dev_doctor

You can turn Iterable into a List with

您可以将 Iterable 转换为 List

List<Element> elements = Lists.newArrayList( getElements() );

回答by Rei

why not:

为什么不:

    Iterable<Element> i = ...; //is what you have
    List<Element> myList = new LinkedList<Element>();
    for (Element e:i) {
        myList.add(e);
    }

? needs no google lib.

? 不需要谷歌库。

回答by Adrian

you can try to place a guard with instanceof:

你可以尝试放置一个警卫instanceof

if (AnElement instanceof AList){
    //cast
    AList = (AnList)Element
}