- 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
Make a Hashtable read-only in Java
Read-only Hashtable would mean users won’t be able to add or remove elements from it. Let us first create a Hashtable with key-value pair −
Hashtable<String, String>hash = new Hashtable<String, String>(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", "E"); hash.put("6", "F"); hash.put("7", "G");
Now, use unmodifiableMap() to form a Hashtable read-only −
Map<String, String>m = Collections.unmodifiableMap(hash);
Example
import java.util.Collections; import java.util.Hashtable; import java.util.Map; public class Demo { public static void main(String[] s) { Hashtable<String, String>hash = new Hashtable<String, String>(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", "E"); hash.put("6", "F"); hash.put("7", "G"); hash.put("8", "H"); hash.put("9", "I"); hash.put("10", "J"); System.out.println("Hashtable = " + hash); Map<String, String>m = Collections.unmodifiableMap(hash); System.out.println("Hashtable is now read-only!"); } }
Output
Hashtable = {9=I, 8=H, 7=G, 6=F, 5=E, 4=D, 3=C, 2=B, 10=J, 1=A} Hashtable is now read-only!
- Related Articles
- How to make a collection read only in java?
- Check if Hashtable is read-only in C#
- How to make Java ArrayList read only?
- How to make an ArrayList read only in Java?
- Golang program to make a file read-only
- Create a Read-Only Collection in Java
- How to create a read-only list in Java?
- Change a file attribute to read only in Java
- How to make the Tkinter text widget read only?
- How to make Laravel (Blade) text field read-only?
- How to make a textarea and input type read only using jQuery?
- Mark file or directory Read Only in Java
- Hashtable in Java
- Java Program to convert a Map to a read only map
- Java Program to convert a list to a read-only list

Advertisements