• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make a collection as unmodifiable

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My team lead asked me that how to make a collection such as a java.util.list as unmodifiable.

can you guys help me out?
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.Collections.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Collections

Edit : Once again I'm much too slow.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shyam sunder prasad wrote:Hi,
My team lead asked me that how to make a collection such as a java.util.list as unmodifiable.
can you guys help me out?

Do you mean making an interface final ? That causes a compiler error.
Ok i will guess. Extend a class such as linked list and mark it final.
Please tell me if i am making mistakes.
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy. Make your own class that implements java.util.List, declare private list variable and use implemented methods of your class to call apptopriate methods of private list variable, except those, which modify list. I.e.:
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrey Kozhanov wrote:It's easy.


And easier still to use java.util.Collections!
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, i forgot another way to make list unmodifiable:You can not do anything with list created this way.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about list.set(0, "Three")? You can't add or remove elements, but replace them - not a problem.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableList%28java.util.List%29
create a list and pass it to the Collections.unmodifieableList method.
below is from doc
Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic