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

此代码将返回以下运行时异常跟踪:
null
java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.remove(Collections.java:1021)

meng.getExps()的Set实现是一个LinkedHashSet。

最佳答案

抱歉,您不走运:Set包裹着Collections.unmodifiableCollection,它确实做到了这一点:使集合不可修改。您唯一可以做的就是将内容复制到另一个Set中并使用它。

07-24 09:33