Friday, December 28, 2007

Read-Only Collections...

Recently I was looking at some open source code and I found out that there is utility function in Collections class which could come handy. When you have mutable Collection object and you don't want anybody to edit (add/delete item) the list, you can just derive immutable version of the same object using the following function..

Collections.unmodifiableList();

If somebody tries to edit the derived list, they will get a neat UnSupportedOperationException. You might wonder why would you need such a list.. If you are working on project with some 50 odd people and you need to make sure that your list (which could be used in some other place) should not be modified by any other fellow programmer, this is the right way to do..!

Happy programming !

This could really come handy in some scenarios as I said above..

Good Bye!

No comments: