什么是“可选操作”在Javadoc中的意思是Set#add(E)? [英] What does "optional operation" mean in Javadoc of for example Set#add(E)?

查看:367
本文介绍了什么是“可选操作”在Javadoc中的意思是Set#add(E)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于什么是“可选操作”在Javadoc中的意思是Set#add(E)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆