简体   繁体   中英

casting exception

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

I am facing a casting exception when I iterate on Set.

Exception is: org.hibernate.collection.PersistentSet cannot be cast to java.util.Iterator . What am I doing wrong?

You don't cast a collection to Iterator . You obtain one: cust.iterator() :

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

(A Collection is Iterable , which defines the iterator() method.)

Iterator seriter = (Iterator)cust; is not a proper casting so an exception is being thrown.

use Iterator seriter = cust.iterator();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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