- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make Java ArrayList read only?
You can make a collection unmodifiable using the unmodifiableList() method of the Collections interface.
Example
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Sample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); System.out.println(list); List myList= (List) Collections.unmodifiableList(list); myList.add("Apache Hadoop"); System.out.println(myList); } }
Output
Exception in thread "main" [JavaFx, Java, WebGL, OpenCV] java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at collections_package.Sample.main(Sample.java:16)
- Related Articles
- How to make an ArrayList read only in Java?
- 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?
- Golang program to make a file read-only
- Check if the ArrayList is read-only in C#
- Creating a read-only wrapper for the ArrayList in C#
- How to make a textarea and input type read only using jQuery?
- How to create a read-only list in Java?
- Change a file attribute to read only in Java
- Create a Read-Only Collection in Java
- Java Program to convert a Map to a read only map
- Java Program to convert a list to a read-only list
- Mark file or directory Read Only in Java

Advertisements