I have a java.lang.Object that I can't cast. When debugging I notice that the Object is an unmodifiable set so I tried to cast it to a set but that didn't work (ClassCastException). Instead I tried
Set<SimpleUserBean> listOfSelectedItems = new HashSet<SimpleUserBean>(object)
But of course that isn't possible either since there is no such constructor for HashSet.
How would I go about solving this?
If the Object is a
java.util.Collections.UnmodifiableSet
or anything else that implementsCollection
, then you should be able to donew HashSet<>((Collection) object)
. That makes it clear to the compiler that you're trying to use theHashSet(Collection<? extends E> c)
constructor.