I have java API which returns java.util.set, I want to iterate over the set till the size-1 and create new java.util.hashset in scala
I tried following :
val keys = CalltoJavaAPI()
val newHashSet = new java.util.HashSet()
val size = keys.size();
newHashSet.add(keys.take(keys.size() - 1))
But I am getting following error:
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractCollection.add(AbstractCollection.java:221)
Tried Following but still not working
val keys = CalltoJavaAPI().asScala
var newHashSet = new scala.collection.mutable.HashSet[Any]()
newHashSet.add(keys.take(keys.size - 1))
UnsupportedOperationExceptionis thrown fromAbstractCollection, which is impossible if you call.add(Object)on aHashSet, because it overrides the behavior (see the source).