This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Collection optional operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collection optional operation" Watch "Collection optional operation" New topic
Author

Collection optional operation

Jonas Dimsa
Greenhorn

Joined: Apr 02, 2007
Posts: 9
hi,

does "optional operation" in interface(etc remove(Object o) in Collection) mean that implementing class has not necessarily to implement that method? if yes, how to create such interface.

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

It means the implementation can just throw an UnsupportedOperationException instead of doing any actual work. The collections returned by Collections.unmodifiableXXX all throw this exception when trying to add, remove or set anything.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
When you want to implement your own Collection, it is often convenient to start from one of the available AbstractX classes.
For example AbstractList. This will give you a default implementation for all the methods you don't have to implement (for example throw an UnsupportedOperationException in the case you don't implement the remove method).
 
 
subject: Collection optional operation
 
Similar Threads
collection interface and other interfaces
Collection
Q about Collection: UnsupportedOperationException
Implementing interfaces
Collections...