Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

java - What does "optional operation" mean in Javadoc of for example Set#add(E)?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...