- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there any method to convert a Set to immutable in Java
Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.
A Set is an interface in collection framework, which does not allow duplicate values.
Method to convert a set to immutable
Yes, Java provides a method named unmodifiableSet() in the Collections class. This method accepts a collection object as a parameter and returns an unmodifiable i.ie immutable form of it.
Example
In the following Java program, we have created a HashSet object and converted it into immutable object using the unmodifiableSet().
import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ImmutableSet { public static void main(String args[]) { Set<Integer> hashSet = new HashSet<Integer>(); //Populating the HashSet hashSet.add(1124); hashSet.add(3654); hashSet.add(7854); hashSet.add(9945); System.out.println(hashSet); //Converting set object to immutable Set immutable = Collections.unmodifiableSet(hashSet); } }
Output
[1124, 3654, 9945, 7854]
Once you convert a set object into immutable, if you try to add elements to it generates a run time exception −
Example
import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ImmutableSet { public static void main(String args[]) { Set<Integer> hashSet = new HashSet<Integer>(); //Populating the HashSet hashSet.add(1124); hashSet.add(3654); hashSet.add(7854); hashSet.add(9945); System.out.println(hashSet); //Converting set object to immutable Set immutable = Collections.unmodifiableSet(hashSet); //Adding elements to the immutable set immutable.add(4466); } }
Run time exception
[1124, 3654, 9945, 7854] Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at MyPackage.ImmutableSet.main(ImmutableSet.java:19)
- Related Articles
- Factory method to create Immutable Set in Java SE 9
- Factory method to create Immutable Set in android?
- How to create an immutable set in Java?
- Is there any alternative for OpenCV imshow() method in Java?
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Map in Java SE 9
- Convert a List to a Set in Java
- Is there any tool that can convert an XSD file to a Python class as JAXB does for Java?
- Convert List to Set in Java
- Convert Stream to Set in Java
- Are there any ways to Manipulate Strings in Java.
- Java Program to convert a List to a Set
- How to convert a Java list to a set?
- How to convert an Array to a Set in Java?
- What is the easiest way to convert a List to a Set in Java?
