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?
It means that an implementor can provide the operation if appropriate, but can also not provide the operation and still be compliant with the intention of the interface.
Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values.
The "third statement" says that abstract interface methods must always be implemented and this remains true in Java 8+. However, as in the Java Collections Framework, it is possible to describe some abstract interface methods as "optional" in the contract.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With