
- 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 make an ArrayList read only in Java?
The unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.
Example
Following Java program creates an ArrayList object, adds elements to it, converts it into a read-only List object.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListReadOnly { public static void main(String[] args) { //Instantiating an ArrayList object ArrayList<String> list = new ArrayList<String>(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); System.out.println(list); //Converting the ArrayList to read only list List<String> myList = (List<String>) Collections.unmodifiableList(list); System.out.println("Array list converted to read only "+list); } }
Output
[JavaFx, Java, WebGL, OpenCV] Array list converted to read only [JavaFx, Java, WebGL, OpenCV]
Once you have retrieved the read-only view of a List object you cannot modify its contents, i.e. you cannot add or delete elements from it directly or using the Iterator object, if you do so an UnsupportedOperationException will be raised.
Example
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListReadOnly { public static void main(String[] args) { //Instantiating an ArrayList object ArrayList<String> list = new ArrayList<String>(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); System.out.println(list); //Converting the ArrayList to read only list List<String> myList = (List<String>) Collections.unmodifiableList(list); myList.add("CoffeeScript"); System.out.println("Array list converted to read only "+myList); } }
Run time exception
[JavaFx, Java, WebGL, OpenCV] Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at SEPTEMBER.remaining.ArrayListReadOnly.main(ArrayListReadOnly.java:20)
- Related Articles
- How to make Java ArrayList read only?
- How to make a collection read only in java?
- Make a Hashtable read-only in Java
- How to make the Tkinter text widget read only?
- How to make Laravel (Blade) text field read-only?
- Check if the ArrayList is read-only in C#
- Creating a read-only wrapper for the ArrayList in C#
- Golang program to make a file read-only
- How to create a read-only list in Java?
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?
- How to make a textarea and input type read only using jQuery?
- How to create an ArrayList from an Array in Java?
- How to replace an element of an ArrayList in Java?
- Change a file attribute to read only in Java

Advertisements