• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Readonly unmodifiable collection

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to create an collection , which can be only read-only & unmodifiable so that others wouldn't be able to modify it.


Set s=new HashSet();
s.add("Sai");
s.add("jothish");



If i want to make this collection read-only how to achieve it?
Any help is appreciated.
Regards.
 
Marshal
Posts: 79417
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a method in the Collections class which does exactly that; it is called somethingUnmodifiableSomething. You get a Collection the user cannot modify (read-only) but the original Collection can still be modified and that alters the read-only version.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How programmatically we can achieve that?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Collections.unmodifiableXXX(...) static methods are the way to achieve this (as Campbell Ritchie explained).

I don't know why you'd want to reinvent the wheel but if you want to create your own version of the unmodifiable views then you need to create a new class that implements one of the collections interfaces. You will then need to provide implementations of all of the methods, mostly delegating to the original collection. Methods that will modify the collection will need to throw some kind of exception (convention is UnsupportedOperationException).
eg


[ October 24, 2008: Message edited by: Paul Beckett ]
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul: don't reinvent the wheel. Instead just use the method mentioned earlier:
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic