• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection optional operation

 
Jonas Dimsa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic