Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes how to make a collection as unmodifiable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to make a collection as unmodifiable " Watch "how to make a collection as unmodifiable " New topic
Author

how to make a collection as unmodifiable

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

Check out java.util.Collections.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

java.util.Collections

Edit : Once again I'm much too slow.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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.

SCJP 6. Learning more now.
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
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

Joined: Sep 07, 2004
Posts: 781

Andrey Kozhanov wrote:It's easy.


And easier still to use java.util.Collections!
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
Oh, i forgot another way to make list unmodifiable:You can not do anything with list created this way.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

How about list.set(0, "Three")? You can't add or remove elements, but replace them - not a problem.
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

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.


Sudheer
SCWCD, SCJP 5
 
 
subject: how to make a collection as unmodifiable
 
Similar Threads
Making a collection modifiable only in a specific class
How do you guard your arrays?
Readonly unmodifiable collection
Making Collection final
how to cache a collection of values