简体   繁体   English

强制例外

[英]casting exception

Set cust = customer.getCustomerBills();
Iterator<Customer> seriter = (Iterator)cust;

I am facing a casting exception when I iterate on Set. 在Set上进行迭代时,我面临强制转换异常。

Exception is: org.hibernate.collection.PersistentSet cannot be cast to java.util.Iterator . 例外是: org.hibernate.collection.PersistentSet cannot be cast to java.util.Iterator What am I doing wrong? 我究竟做错了什么?

You don't cast a collection to Iterator . 您不会将集合IteratorIterator You obtain one: cust.iterator() : 您获得一个: cust.iterator()

Set<Customer> cust = customer.getCustomerBills();
Iterator<Customer> seriter = cust.iterator();

(A Collection is Iterable , which defines the iterator() method.) CollectionIterable ,它定义了iterator()方法。)

Iterator seriter = (Iterator)cust; Iterator seriter =(Iterator)cust; is not a proper casting so an exception is being thrown. 是不正确的强制转换,因此将引发异常。

use Iterator seriter = cust.iterator(); 使用Iterator seriter = cust.iterator();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM