views:

61

answers:

2
final Set<Expression> exps = meng.getExps();
Iterator<Expression> iterator = exps.iterator();
final Expression displayedExp = exps.iterator().next();
exps.remove(displayedExp);

This code would return the following run-time exceptions trace:

null
java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.remove(Collections.java:1021)

The Set implementation of meng.getExps() is a LinkedHashSet.

+3  A: 

Your getter is explicitly returning you an UnmodifiableCollection, which is a wrapper of sorts around Sets that prevents modification.

In other words, the API is telling you "this is my collection, please look but don't touch!"

If you want to modify it, you should copy it into a new Set. There are copying constructors for HashSet that are great for this purpose.

Steven Schlansker
+3  A: 

Sorry, you are out of luck: The Set was wrapped with Collections.unmodifiableCollection, which does exactly this: making the collection unmodifiable. The only thing you can do is copy the content into another Set and work with this.

Landei
and is there any utility like Collection.copy to do that?
simpatico
E.g. Set<Expression> set = new HashSet<Expression>(exps);
Landei