I called a getElements
method which returns Iterable<Element>
.
I did this:
List<Element> elements = (List<Element>) getElements();
This generates the error:
java.lang.ClassCastException: com.utesy.Element$3
cannot be cast to java.util.List
I thought a List
was a type of Iterable
?
You can turn Iterable into a List with
List<Element> elements = Lists.newArrayList( getElements() );
Yes, List<T>
extends Iterable<T>
, but that doesn't mean that you can cast from any Iterable<T>
to List<T>
- only when the value actually refers 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?
To put it in simpler terms, let's change Iterable<T>
to Object
and List<T>
to String
. String
extends Object
, so you can try to cast from Object
to String
... but the cast will only succeed at execution time if the reference actually refers to a String
(or is null).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With