
- 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 to initialize immutable collections in Java 9?
Java 9 provides factory methods to create immutable lists, sets, and maps. It can be useful to create empty or non-empty collection objects. In Java 8 and earlier versions, we can use collection class utility methods like unmodifiableXXX to create immutable collection objects. If we need to create an immutable list then use the Collections.unmodifiableList() method.
These factory methods allow us for easy initialization of immutable collections whether they are empty or non-empty.
Initialization of immutable list:
List<Integer> immutableEmptyList = List.of();
In the above, we have initialized an empty, immutable List.
Initialization of immutable set:
Set<Integer> immutableEmptySet = Set.of();
In the above, we have initialized an empty, immutable Set.
Initialization of immutable map:
Map<Integer, Integer> immutableEmptyMap = Map.of();
In the above, we have initialized an empty, immutable Map.
Example
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; public class ImmutableCollectionTest { public static void main(String args[]) { List<String> list8 = new ArrayList<String>(); list8.add("INDIA"); list8.add("AUSTRALIA"); list8.add("ENGLAND"); list8.add("NEWZEALAND"); List<String> immutableList8 = Collections.unmodifiableList(list8); immutableList8.forEach(System.out::println); System.out.println(); List<String> immutableList = List.of("INDIA", "AUSTRALIA", "ENGLAND", "NEWZEALAND"); immutableList.forEach(System.out::println); System.out.println(); Set<String> immutableSet = Set.of("INDIA", "AUSTRALIA", "ENGLAND", "NEWZEALAND"); immutableSet.forEach(System.out::println); System.out.println(); Map<String, String> immutableMap = Map.of("INDIA", "India", "AUSTRALIA", "Australia", "ENGLAND", "England", "NEWZEALAND", "Newzealand"); immutableMap.forEach((key, value) -> System.out.println(key + " : " + value)); System.out.println(); } }
Output
INDIA AUSTRALIA ENGLAND NEWZEALAND INDIA AUSTRALIA ENGLAND NEWZEALAND AUSTRALIA ENGLAND NEWZEALAND INDIA AUSTRALIA : Australia ENGLAND : England NEWZEALAND : Newzealand INDIA : India
- Related Articles
- What are the benefits of immutable collections in Java 9?
- How to initialize an array in JShell in Java 9?
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Map in Java SE 9
- Factory method to create Immutable Set in Java SE 9
- Which factory methods have added for collections in Java 9?
- How to create immutable class in Java?
- How to create the immutable class in Java?
- How to create an immutable class in Java?
- How to create an immutable set in Java?
- How to initialize an array in Java
- How to make elements of array immutable in Java?
- how to initialize a dynamic array in java?
- How to Initialize and Compare Strings in Java?
- Immutable String in Java
