Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java unmodifiableSet Method | Immutable set in Java

BACK

The unmodifiableSet() method Returns an Unmodifiable view of the specified set in Descending order

Example Program

import java.util.*;

public class UnmodifiableSetclt {
public static void main(String[] args) {
// create hashset object
Set set = new HashSet();
// set the value in hashset
set.add("xiva");
set.add("yelcom");
set.add("zava");
// display the initial value in set
System.out.println("Initial set value: " + set);
// make the set unmodifiable
Set unmodset = Collections.unmodifiableSet(set);
// try to modify the set
unmodset.add("hello");
}

}

Output:

 Initial set value: [zava, yelcom, xiva]Exception in thread “main” Java.lang.UnsupportedOperationException

at java.util.Collections$UnmodifiableCollection.add(Unknown Source)

at SecondTask.UnmodifiableSetclt.main(UnmodifiableSetclt.java:18)

Explanation:

The unmodifiableSet() method Returns an unmodifiable view of the specified set in Descending order

Parameter:

This is the set for which an unmodifiable view is to be returned.

Return:

       The method call returns an unmodifiable view of the specified set.

Throw: NA

BACK



This post first appeared on Core Java,Servlet,Jsp,Struts,Hibernate,Spring Fram, please read the originial post: here

Share the post

Java unmodifiableSet Method | Immutable set in Java

×

Subscribe to Core Java,servlet,jsp,struts,hibernate,spring Fram

Get updates delivered right to your inbox!

Thank you for your subscription

×