什么是“可选操作”在Javadoc中的意思是Set#add(E)?

问题描述:

Set 的java文档中>它在方法的规范中说可选操作例如(我强调)

When in the java documentation for Set it says in the specification of a method Optional Operation e.g. (emphasis by me)


add(E e)

如果不是,则将指定的元素添加到此集合中已经存在(可选操作)

这里的可选含义是什么?

What does the optional mean here?

如果我使用SUN / Oracle以外的JVM,Java的实现可能无法提供此操作吗?

That if I use a JVM other than SUN/Oracle, this operation may not be provided by that implementation of Java?

设置是一个界面。实现该接口的类不一定需要为可选操作提供实现。

Set is an interface. Classes implementing that interface do not necessarily need to provide an implementation for an optional operation.

我认为这些可选操作返回到一般 Collection 接口,其中操作是可选的,对某些种类的集合没有意义。例如。 add 是一种对某种只读集合没有用处的操作。它在Javadoc中明确地拼写出来,因此它成为所有集合类提供的一部分但是使用它的人知道,鉴于某些集合他们并不确切知道,可能是该方法只是抛出一个 UnsupportedOperationException

I think those optional operations go back to the general Collection interface where operations are made optional which do not make sense for some kinds of collections. E.g. add is an operation that isn't really useful on some kind of read-only collection. It's spelt out explicitly in the Javadoc so it becomes part of what all collection classes offer but someone using it knows that, given some collection they do not know exactly, it could be that the method just throws an UnsupportedOperationException.