views:

104

answers:

7

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?

A: 

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

Take any Set for instance, a HashSet is Iterable but the elements has no order, so it can't implement the List interface, and is thus not a List.

aioobe
`<nit-pick>` "but the elements has no order, so it's not a `List`" - that's not the reason that a `HashSet` isn't a `List`. A `HashSet` isn't a `List` because it doesn't implement the `List` interface. `</nit-pick>`
Matt Ball
@Matt Ball, true... reformulated a bit... better? :)
aioobe
+2  A: 

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

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

org.life.java
+3  A: 

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

Jon Skeet
+2  A: 

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

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

John Pickup
+1  A: 

List is a subinterface of Iterable meaning 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.

Try to avoid that sort of casting.

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

posdef
+6  A: 

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.

Erick Robertson
+1 for not having 229k rep
willcodejavaforfood
A: 

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

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.

AshirusNW
List implements Iterable, and this *does* mean that it can be cast to it. The problem the OP is having is going the other way. This has nothing to do with it being an implement vs extend.
Erick Robertson
Sorry, the "doesn't mean it can be cast it" was unclear, I've filled in the "it"s so it can't be wrongly interpreted as you have.
AshirusNW