
- 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 List in Java 9?
A list considered to be unmodifiable if the elements can't be added, removed, or replaced from a list once an unmodifiable instance of a list has created. The static factory method: List.of() provides a convenient way to create unmodifiable lists in Java 9.
An instance of a list created by using the List.of() method has the following characteristics.
- The list returned by a factory method is conventionally immutable. It means that the elements can't be added, removed, or replaced from a list. Calling any mutator method on the List causes UnsupportedOperationException.
- If the contained elements of List are mutable, it may cause the List's contents to appear to change.
- An immutable list 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.
- An unmodifiable lists are serializable if all elements are serializable.
- The order of elements in a list is the same as the order of the provided parameters, or of the elements in the provided array.
Syntax
List.of(E... elements)
Example
import java.util.List; public class UnmodifiedListTest { public static void main(String[] args) { List<String> countries = List.of("India", "Australia", "England", "Newzealand"); System.out.println("Countries - " + countries); countries.add("Srilanka"); // throws UnsupportedOperationException } }
Output
Countries - [India, Australia, England, Newzealand] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(Unknown Source) at java.base/java.util.ImmutableCollections$AbstractImmutableList.add(Unknown Source) at UnmodifiedListTest.main(UnmodifiedListTest.java:7)
- Related Articles
- How can we create an unmodifiable Set 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?
- How can we create a multi-release jar(mrjar) using jar tool in Java 9?
- Can we use private methods in an interface in Java 9?
- Can we create an object of an abstract class in Java?
- Can we convert a list to an Array in Java?
- Can we create an enum with custom values in java?
- How can we create MySQL views with column list?
- How can we convert list to Set in Java?

Advertisements