In this tutorial, we will learn how to make immutable ArrayList with an example.
We will see two examples to make immutable ArrayList:
In this tutorial, we have seen how to make immutable ArrayList using Collections.unmodifiableList() method and Java 9 List.of() factory method with an example.
We will see two examples to make immutable ArrayList:
- First, we will see how to create an immutable ArrayList using Collections.unmodifiableList() method with an example
- Second, we will see how to create the Immutable ArrayList with Java 9 List.of() factory method example.
Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
In the following, we create an immutable ArrayList using Collections.unmodifiableList() method with an example
public class ImmutableListExample {
public static void main(String[] args) {
// Creating an ArrayList of String using
List < String > fruits = new ArrayList < > ();
// Adding new elements to the ArrayList
fruits.add("Banana");
fruits.add("Apple");
fruits.add("mango");
fruits.add("orange");
fruits = Collections.unmodifiableList(fruits);
// Creating Immutable List
//fruits.add("Strawberry"); // Exception in thread "main"
// java.lang.UnsupportedOperationException<String> fruits = List.of("Banana", "Apple", "Mango", "Orange");
fruits.forEach(e - > System.out.println(e));
}
}
Output:
Banana
Apple
mango
orange
Create the Immutable ArrayList Example - Java 9 List.of() Method
Let's use List.of() static factory methods to create immutable ArrayList.
package net.javaguides.corejava.java9;
import java.util.List;
/**
* Java 9 Immutable List Example
* @author Ramesh Fadatare
*
*/
public class ImmutableListExample {
public static void main(String[] args) {
// Creating Immutable List
List < String > fruits = List.of("Banana", "Apple", "Mango", "Orange");
fruits.forEach(e - > System.out.println(e));
// You can't add Elements Immutable List
// fruits.add("Strawberry"); // Exception in thread "main" java.lang.UnsupportedOperationException
// in single list
List < String > list = List.of("A", "B", "C", "D");
list.forEach(e - > System.out.println(e));
}
}
Output:
Banana
Apple
mango
orange
A
B
C
D
Conclusion
Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
Comments
Post a Comment
Leave Comment