Javatpoint Logo
Javatpoint Logo

How to Create an Unmodifiable List in Java?

In Java, when we want to be sure that a list's contents cannot be changed after it is created, there are situations in which creating an unmodifiable list in Java might be very important. In this section, we will discuss how to create an unmodifiable List in Java.

Why Unmodifiable Lists?

Prior to exploring the techniques, let's examine the importance of immutable lists. If we wish to impose immutability on your code, then unmodifiable lists are important. A list's immutability guarantees that once formed, its elements cannot be altered. It helps in situations when we need thread safety, data integrity, or the ability to distribute a list without running the risk of accidental changes.

The unmodifiableList() method in Java collections

An easy approach to get an unmodifiable view of an existing list in Java is to use the Collections class's unmodifiableList() function. Any attempt to alter the list-directly or through its iterator-will raise an UnsupportedOperationException as this view forbids changes to the list.

Syntax

Importance of Immutable Lists

1. Data Integrity via Immutability

An object's immutability ensures that its state, in this case a list, cannot be altered once it is generated. It suggests that lists have components that, once established, do not alter, providing data with a dependable and consistent presentation.

2. Twill safety

Concurrent programming thread safety is enhanced via immutable lists. Explicit synchronisation techniques are not necessary for accessing or traversing these lists in a multi-threaded environment since they cannot be updated. Because of its intrinsic thread safety, concurrent programming is made easier and the chance of data corruption is decreased.

3. Safe List Removal

Unmodifiable lists provide a certain amount of security when transferring lists between methods or between various parts of a programme.

Method 1: Using Collections.unmodifiableList() Method

The Java Collections class has the unmodifiableList() utility function that may be used to produce an unmodifiable view of a list. How to use this approach is as follows:

UnmodifiableList.java

Output:

Unmodifiable List: [Java, Python, C++]

Method 2: Using List.copyOf() Method

Unmodifiable.java

Output:

Unmodifiable List: [Maths, Physics, English]

In line with Collections.any effort to change the unmodifiableList that was made using List by calling unmodifiableList().There will be an UnsupportedOperationException when using copyOf() method.

Method 3: Using Arrays.asList() Method

An array may be converted into an immutable list using the Arrays.asList() function. Changes made to the original array will have an impact on the list and vice versa, even if the list itself cannot be changed.

Unmodifiable.java

Output:

Unmodifiable List: [One, Two, Three]

Method 4: Using List.of() Method in Java 9

The List.of() factory method that was introduced in Java 9, lets you generate an unmodifiable list right away.

Unmodifiable.java

Output:

Unmodifiable List: [Dell, Apple, Hp]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA