2:21 am - Friday April 19, 2019

Java unmodifiableSet Method | Immutable set in Java

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

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

Filed in: Z-A All Java Codes

Comments are closed.