aspose file tools
The moose likes Java in General and the fly likes ClassCastException with JDK7U5 while using Iterator. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "ClassCastException with JDK7U5 while using Iterator." Watch "ClassCastException with JDK7U5 while using Iterator." New topic
Author

ClassCastException with JDK7U5 while using Iterator.

Nilesh Sahu
Greenhorn

Joined: Dec 11, 2012
Posts: 1
Hi All,

While i am trying to iterate through a raw Collection (without generics) which contains my business objects using Iterator. It is throwing ClassCastException when i am casting the Iterator's next value with expected object.
but when i replace Iterator with for(int i=0; i<collection.size();i++) loop its working fine and i am able to iterate the collection without any exception and i am able to cast my object with expected class type.

I am not sure why this is happening with JDK1.7u5 while the same code(with iterator) works fine in JDK1.6u31.

Please suggest me the solution or workaround for the same.

Thanks .



Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2539
    
  10

Hi Nilesh, and welcome to JavaRanch!

I'd be extremely surprised if casting an object to a type in its hierarchy caused a ClassCastException in any version of Java, and just as surprised if casting an object to a type not in its hierarchy didn't cause one. Of course, Iterator is just an interface, so a customized Iterator could be doing anything, including changing the types of the elements it iterates over. Can you give us an example that demonstrates this problem?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ClassCastException with JDK7U5 while using Iterator.
 
Similar Threads
Fast fail behavior of collection
Threadsafe iteration over non-indexed collection?
Iterator vs for loop
EJB HELP
Removing entries from a collection