Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional method in Collection interface [duplicate]

What does "optional" mean in this context?

public interface Collection<E> extends Iterable<E> 
{
 // Basic operations
   int size();
   boolean isEmpty();
   boolean contains(Object element);
 // optional

The last line comment "//optional": what does it mean? I tried to implement a Collection by implementing it public class Col-implementation implements Collection and it's not optional at all.

I need to implement all the methods, even the ones commented as "optional". I am clearly not getting what they mean by "optional". Any insight about that?

like image 267
Rollerball Avatar asked Dec 12 '25 21:12

Rollerball


1 Answers

You must provide an implementation, but that implementation can throw an UnsupportedOperationException.

like image 110
Peter Lawrey Avatar answered Dec 15 '25 12:12

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!