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.
To remove an item in a set, use the remove() , or the discard() method.
Method 1: Use of discard() method The built-in method, discard() in Python, removes the element from the set only if the element is present in the set. If the element is not present in the set, then no error or exception is raised and the original set is printed.
set::erase() erase() function is used to remove elements from a container from the specified position or range.
There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.
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.
Your getter is explicitly returning you an UnmodifiableCollection
, which is a wrapper of sorts around Set
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With