“Iterable cannot be cast to List” - Isn't `List` a type of `Iterable`?

后端 未结 10 1669
生来不讨喜
生来不讨喜 2021-02-20 00:27

I called a getElements method which returns Iterable.

I did this:

List elements = (List

        
10条回答
  •  太阳男子
    2021-02-20 01:10

    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.

提交回复
热议问题