BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 9 – Factory methods to create Immutable Set

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last tutorial, we learned how to create immutable lists with ease using the factory methods introduced in Java 9. In this guide, we will see the use of newly introduced factory methods to create immutable Sets.

1. Creating immutable Set prior to Java 9

Before we discuss how to use the factory methods to create unmodifiable Sets, lets see how we used to create immutable set prior to Java 9.

1.1 Creating empty immutable Set before java SE 9

Prior to Java 9, we have to use unmodifiableSet() method of Collections class to create immutable Set. In the following example we are creating an empty set.

Set<String> emptyHashSet = new HashSet<String>();
Set<String> immutableHSet = Collections.unmodifiableSet(emptyHashSet);

Lets test this code in JShell (a new tool introduced in Java 9)

Creating Empty Set Before Java 9

1.2 Creating Non-empty immutable Set before Java SE 9

This is how we used to create non empty immutable Sets before Java 9. As you can see we have to write several lines of code to get this working. In Java 9 we can write this code in a single line, we will see that in the next section.

Set<String> hset = new HashSet<String>();
hset.add("Jon Snow");
hset.add("Khal Drogo");
hset.add("Daenerys");
Set<String> immutableSet = Collections.unmodifiableSet(hset);

Non Empty immutable Set before java 9

2. Java 9 – Creating Immutable Set using static factory method of()

Java 9 introduced couple of versions of of() method to create unmodifiable Sets.

static <E> Set<E> of()

2.1 Java 9 – Creating empty immutable Set

Set<String> immutableSet = Set.of();

Creating Empty Set in Java 9 using Factory Methods

2.2 Java 9 – Creating Non-empty immutable Set

As you can see how simple it is to create immutable set in Java 9.

Set<String> immutableSet = Set.of("Apple", "Banana", "Orange");

Creating Non Empty Set in Java 9 using method of()

3. What is an immutable Set?

1. An immutable Set doesn’t allow the add, delete and update of its elements, if we try to do that then we will get UnsupportedOperationException Exception. Lets take an example to see this.

jshell> Set immutableSet = Set.of("Paul", "Lora", "Steve");
immutableSet ==> [Paul, Lora, Steve]

jshell> immutableSet.add("Harry")
|  java.lang.UnsupportedOperationException thrown: 
|        at ImmutableCollections.uoe (ImmutableCollections.java:71)
|        at ImmutableCollections$AbstractImmutableSet.add (ImmutableCollections.java:281)
|        at (#2:1)

2. We cannot add null elements to an immutable Set.

jshell> Set immutableSet = Set.of("Paul", "Lora", "Steve");
immutableSet ==> [Lora, Steve, Paul]

jshell> immutableSet.add(null)
|  java.lang.UnsupportedOperationException thrown: 
|        at ImmutableCollections.uoe (ImmutableCollections.java:71)
|        at ImmutableCollections$AbstractImmutableSet.add (ImmutableCollections.java:281)
|        at (#2:1)

Top Related Articles:

  1. Java 9 – Factory method to create Immutable List
  2. java 9 JShell – Working with Methods
  3. LinkedHashMap in Java
  4. Java 9 Features with Examples
  5. java 9 JShell – Working with variables

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap