I have a problem after I convert List<MyCustomObject> to a HashSet<MyCustomObject>. When I try to iterate through each element using an enhanced for I get a ClassCastException stating that “java.util.HashMap cannot be cast to MyCustomObject”.
Here is the code:
List<MyCustomObject> myList = remoteMethod.getList();//the list is obtained via a REST call so it does go through the serialize/deserialize process...
Set<MyCustomObject> mySet = new HashSet<MyCustomObject>(myList);
for(MyCustomObject object : mySet) <----this is where it goes boooom!!!
{
DB.add(object);
}
So before the enhanced for statement I extracted the first object in the list and used .getClass and it DID come up as HashMap. Is something happening during serialization/deserialization? Or is it in the conversion of the List to Set? What could be going wrong, and how would I fix it?
Whatever is putting the data into the
ListthatremoteMethod.getList()returns is puttingHashMaps into it instead ofMyCustomObjects. You should change it to putMyCustomObjects into theList.The contents of the list are not guaranteed to be
MyCustomObjectinstances at runtime, and that’s probably what’s creating your confusion.