Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Unmodifiable set' - 2 code snippet(s) found

 Sample 1. Get a unmodifiable / read only Set

Set<String> modifiableSet = Sets.newHashSet();

modifiableSet.add("New York");
modifiableSet.add("Washington");
modifiableSet.add("Georgia");

Set<String> unmodifiableSet = Collections.unmodifiableSet(modifiableSet);

   Like      Feedback     collections  set  unmodifiable set   unmodifiableset   Collections.unmodifiableSet


 Sample 2. Create an UnmodifiableMultiSet ( Read only set allowing multiple values ) using Apache Commons

MultiSet<String> multiSet = new HashMultiSet(); 
multiSet.add("Albama");
multiSet.add("Albama");
multiSet.add("Albama");

System.out.println(multiSet); // Prints [Albama:3]
      
UnmodifiableMultiSet<String> unmodifiablemultiSet = (UnmodifiableMultiSet<String>) MultiSetUtils.unmodifiableMultiSet(multiSet);

unmodifiablemultiSet.add("Albama"); // throws java.lang.UnsupportedOperationException

   Like      Feedback     UnmodifiableMultiSet  Apache Commons Collections  Set  MultiSet



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus