
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Is there any alternative for OpenCV imshow() method in Java?
- 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?
- Are there any ways to Manipulate Strings in Java.
- Is there any JSTL library to parse XML in a JSP?
- Is there any tool that can convert an XSD file to a Python class as JAXB does for Java?
- Is there any alternative solution for static constructor in java?
- Is there any Python object inspector?
- Is there a MySQL command to convert a string to lowercase?
- Is there any way to skip some documents in MongoDB?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Convert a List to a Set in Java
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Map in Java SE 9