• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 80947
522
  • 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus 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:
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic