“可选操作”是什么意思?例如 Set#add(E) 在 Javadoc 中的意思是什么?
当在 Set 的 java 文档中时,它说在方法的规范中可选操作
例如(我强调)
添加(E e)
如果指定元素尚不存在,则将其添加到该集合中(可选操作)。
这里的可选是什么意思?
如果我使用 SUN/Oracle 以外的 JVM,该操作可能无法由该 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)
Adds the specified element to this set if it is not already present (optional operation).
What does the optional mean here?
That if I use a JVM other than SUN/Oracle, this operation may not be provided by that implementation of Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Set
是一个接口。实现该接口的类不一定需要提供可选操作的实现。我认为这些可选操作可以追溯到通用的
Collection
接口,其中操作是可选的,这对于某些类型的集合没有意义。例如,add
是一个对于某种只读集合来说并不是真正有用的操作。它在 Javadoc 中明确说明,因此它成为所有集合类提供的内容的一部分,但使用它的人知道,给定一些他们不确切知道的集合,该方法可能只是抛出一个 <代码>UnsupportedOperationException。Set
is an interface. Classes implementing that interface do not necessarily need to provide an implementation for an optional operation.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 anUnsupportedOperationException
.来自 java.util.Collections 文档:
请注意,其中描述的许多方法都不是可选的。
可以说,Java 集合框架并不完美。这可能是突出其(微小)头的缺陷之一。
From the java.util.Collections documentation:
Note that many of the methods described there are not optional.
The Java Collections Framework is, arguably, not perfect; this may be one of the imperfections rearing its (tiny) head.
在 JavaDoc 中将接口方法指定为可选意味着实现此接口的类不一定必须实现该方法。相反,他们可以抛出异常。
更具体地说,接口方法在 JavaDoc 中是可选并不意味着它是特定于实现的行为。该类的每个具体实现都会指定它是否实现它。查看 HashMap class 它包含添加操作,但没有将其指定为可选。因此,Java 库的每个实现都必须为其
HashMap
类包含此方法的实现。 TreeMap 等也是如此。将此操作声明为可选的原因可能是因为某些集合在概念上可能是不可变的,例如由Collections.unmodifyingSet< /a>
That an interface method is specified as optional in the JavaDoc means that classes implementing this interface does not necessarily have to implement that method. Instead, they could for example, throw an exception.
More specifically, that an interface method is optional in the JavaDoc does not mean that it is implementation-specific behavior. Each concrete implementation of the class will specify whether it implements it or not. Looking at the HashMap class it includes the add operation and it does not specify it as optional. Thus, every implementation of the Java library will have to include an implementation of this method for their
HashMap
class. The same goes forTreeMap
etc.The reason why it might make sense for this operation to be declared as optional is because some sets may be conceptually immutable, such as those returned by Collections.unmodifiableSet