
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we create an unmodifiable Set in Java 9?
The immutable static factory method Set.of() can provide a convenient way to create unmodifiable sets in Java 9.
An instance of a set created by using the Set.of() method has the following characteristics.
- The set returned by a factory method is conventionally immutable. It means that elements can't be added, removed, or replaced from a Set. Calling of any mutator method on the Set causes UnsupportedOperationException.
- If the contained elements of Set are mutable, it may cause the Set's contents to appear to change.
- An immutable set can be created using static factory methods that don't allow null elements. If we are trying to create with null elements, it throws NullPointerException.
- It rejects duplicate elements at the time of immutable set creation. The duplicate elements passed to a static factory method results in IllegalArgumentException.
- The order of iteration of set elements is unspecified and subject to change.
Syntax
Set.of(E... elements)
Example
import java.util.Set; public class SetOfMethodTest { public static void main(String args[]) { Set<String> names = Set.of("Adithya", "Bhavish", "Chaitanya", "Jai"); System.out.println("Names - " + names); names.add("Raja"); // throws UnsupportedOperationException } }
Output
Names - [Bhavish, Adithya, Jai, Chaitanya] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(Unknown Source) at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(Unknown Source) at SetOfMethodTest.main(SetOfMethodTest.java:8)
- Related Articles
- How can we create an unmodifiable List in Java 9?
- How can we create an unmodifiable Map in Java 9?\n
- How to create an unmodifiable map in Java?
- How can we create an instance of VarHandle in Java 9?
- How can we create a Service Provider interface in Java 9?
- How can we modify an existing module in Java 9?
- Can we create an object for an interface in java?
- How can we get an ID of the running process in Java 9?
- Can we use private methods in an interface in Java 9?
- How can we create a multi-release jar(mrjar) using jar tool in Java 9?
- Can we create an object of an abstract class in Java?
- Can we create an enum with custom values in java?
- How can we implement the SubmissionPublisher class in Java 9?
- How can we display all module names in Java 9?
- How can we implement the Subscriber interface in Java 9?

Advertisements