I have a wrapper layer which returns me a Collection of objects of certain Type. Now I call this wrapper in my application which has a different type, so I try to cast the whole collection that is been returned into my application type.
Collection<wrapperType> wrapperCollection = wrapper.method(); MyApp<appType> appCollection = (Collection<appType>)wrapperCollection; Above casts wrapperCollection to appCollection. Now when i try to do this-:
for( appType item : appCollection) // Exception-: can't convert wrapperType to appType. { ....... } I am trying to figure out-:
- How can I cast wrapperCollection to appCollection so that each item inside the appCollection becomes appType and then I can iterate over it? Is there any other better way via which I can easily cast ans Iterate over collection.
- I am also thinking is casting a good idea at all performance wise.
I researched following links-:
MyApp,wrapperType, andappTypewrapperCollectioncontains something that's not anappType. (Perhaps you need to learn more about inheritance and casting in general here...?) docs.oracle.com/javase/tutorial/java/IandI/subclasses.html