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

发布于 2024-12-19 08:37:39 字数 332 浏览 1 评论 0原文

当在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心舞飞扬 2024-12-26 08:37:39

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 an UnsupportedOperationException.

天冷不及心凉 2024-12-26 08:37:39

来自 java.util.Collections 文档:

该接口中包含的“破坏性”方法,即
修改它们操作的集合的方法是
如果此集合指定抛出 UnsupportedOperationException
不支持操作。如果是这种情况,这些方法
可以(但不是必须)抛出 UnsupportedOperationException
如果调用对集合没有影响。例如,
对不可修改的集合调用 addAll(Collection) 方法
如果集合为
待添加内容为空。

请注意,其中描述的许多方法都不是可选的。

可以说,Java 集合框架并不完美。这可能是突出其(微小)头的缺陷之一。

From the java.util.Collections documentation:

The "destructive" methods contained in this interface, that is, the
methods that modify the collection on which they operate, are
specified to throw UnsupportedOperationException if this collection
does not support the operation. If this is the case, these methods
may, but are not required to, throw an UnsupportedOperationException
if the invocation would have no effect on the collection. For example,
invoking the addAll(Collection) method on an unmodifiable collection
may, but is not required to, throw the exception if the collection to
be added is empty.

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.

最丧也最甜 2024-12-26 08:37:39

在 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 for TreeMap 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文