Read alone List, Map as well as Set inwards Java
A read alone List way a List where you lot tin non perform modification operations similar add, remove or set. You tin alone read from the List past times using larn method or past times using Iterator of List, This variety of List is expert for a certainly requirement where parameters are concluding as well as tin non hold upwards changed. In Java, you lot tin role Collections.unModifiableList() method to create read alone List , Collections.unmodifiableSet() for creating read-only Set similar read alone HashSet as well as similarly creating a read-only Map inwards Java, every bit shown inwards below example. Any modification inwards read alone List volition lawsuit inwards java.lang.UnSupportedOperationException in Java. This read-only List instance is based on Java five generics but besides applicable to other Java versions similar JDK 1.4 or JDK 1.3, merely take Generics code i.e. angle bracket which is non supported prior to Java 5. One mutual error programmer makes is that assuming fixed size List as well as read alone List every bit same.
As shown inwards our 3 instance of converting Array to Array List , nosotros tin role Arrays.asList() method to create as well as initialized List at same line. List implementation returned past times this method is fixed size as well as it doesn’t allow adding or removal of chemical ingredient but it is non read alone because you lot tin update objects past times calling set(index) method. How to brand a collection read alone is besides a popular Java collection interview question, which makes this Java collection tutorial fifty-fifty to a greater extent than important.
As shown inwards our 3 instance of converting Array to Array List , nosotros tin role Arrays.asList() method to create as well as initialized List at same line. List implementation returned past times this method is fixed size as well as it doesn’t allow adding or removal of chemical ingredient but it is non read alone because you lot tin update objects past times calling set(index) method. How to brand a collection read alone is besides a popular Java collection interview question, which makes this Java collection tutorial fifty-fifty to a greater extent than important.
Read alone List, Set as well as Map Example - Java
Here is sample Java programme which demonstrate method of creating read alone List, Set as well as Map inwards Java. You tin brand whatsoever List, Set or Map implementation every bit read alone past times next code example. Just scream upwards that Collections.unModifiableList() , Collections.unModifiableSet() as well as Collections.unModifiableMap() method inwards Java.
ArrayList, LinkedList or Vector to read alone ArrayList , LinkedList as well as Vector inwards Java.
ArrayList, LinkedList or Vector to read alone ArrayList , LinkedList as well as Vector inwards Java.
package example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Java programme to create read alone List, Set as well as Map inwards Java. You tin commencement create
* a List or Set as well as than travel into unmodifiable or read-only past times
* using Collections.unmodifiableList() or Collections.unmodifiableSet() method.
*
* @author Javin Paul
*/
public class ReadOnlyListSetMap {
public static void main(String args[]) {
// creating List inwards Java
List<String> contents = new ArrayList<String>();
// initializing List inwards Java
contents.add("Example");
contents.add("Tutorial");
contents.add("Program");
// Currently This List is non read only, you lot tin add together or take elements from List
contents.add("Tips"); //should non hold upwards allowed if List is read only.
System.err.println("normal List inwards Java : " + contents);
//creating readonly List from contents
contents = Collections.unmodifiableList(contents);
//java.lang.UnsupportedOperationException -- no modification inwards read alone list
//not allowed every bit it is read-only List inwards Java
contents.add("Can I add together object into read alone List - No");
contents.remove("Example"); //remove non allowed inwards read alone list
//java.lang.UnSupportedOperation - List update non allowed
contents.set(0, "Can I override or laid object inwards read-only Set - No");
//Creating read alone Set inwards Java
//similar to read-only List you lot tin besides create a Set which is read alone
//i.e. improver , removal as well as modification functioning is non permitted on list
//Creating a Set based on contents of List
Set<String> readOnlySet = new HashSet<String>(contents);
System.out.println("original Set inwards Java : " + readOnlySet);
//Set is non yet read-only you lot tin nonetheless add together elements into Set
readOnlySet.add("Override");
System.out.println("Set earlier making read alone : " + readOnlySet);
//making Set readonly inwards Java - no add together take or laid functioning permitted
readOnlySet = Collections.unmodifiableSet(readOnlySet);
//trying to add together chemical ingredient inwards read alone Set - java.lang.UnSupportedOperationException
readOnlySet.add("You tin non add together chemical ingredient inwards read Only Set");
//trying to take chemical ingredient from read alone set
readOnlySet.remove("Example"); //you tin non take elements from read alone Set
// Creating read alone Map inwards Java
// Similar to List as well as Set you lot tin besides create read alone or unmodifiable Map inwards Java
// add together , take as well as override is non allowed on read alone Map inwards Java
Map<String, String> contries = new HashMap<String, String>();
contries.put("India", "New Delhi");
//Map is non read alone yet, you lot tin nonetheless add together entries into
contries.put("UK", "London");
System.out.println("Map inwards Java earlier making read only: " + contries);
//Making Map read alone inwards Java
Map readOnlyMap = Collections.unmodifiableMap(contries);
//you tin non lay a novel entry inwards read alone Map inwards Java
readOnlyMap.put("USA", "Washington"); //java.lang.UnSupportedOperation
//you tin non take keys from read alone Map inwards Java
readOnlyMap.remove("UK"); //java.lang.UnSupportedOperation
}
}
*
* @author Javin Paul
*/
public class ReadOnlyListSetMap {
public static void main(String args[]) {
// creating List inwards Java
List<String> contents = new ArrayList<String>();
// initializing List inwards Java
contents.add("Example");
contents.add("Tutorial");
contents.add("Program");
// Currently This List is non read only, you lot tin add together or take elements from List
contents.add("Tips"); //should non hold upwards allowed if List is read only.
System.err.println("normal List inwards Java : " + contents);
//creating readonly List from contents
contents = Collections.unmodifiableList(contents);
//java.lang.UnsupportedOperationException -- no modification inwards read alone list
//not allowed every bit it is read-only List inwards Java
contents.add("Can I add together object into read alone List - No");
contents.remove("Example"); //remove non allowed inwards read alone list
//java.lang.UnSupportedOperation - List update non allowed
contents.set(0, "Can I override or laid object inwards read-only Set - No");
//Creating read alone Set inwards Java
//similar to read-only List you lot tin besides create a Set which is read alone
//i.e. improver , removal as well as modification functioning is non permitted on list
//Creating a Set based on contents of List
Set<String> readOnlySet = new HashSet<String>(contents);
System.out.println("original Set inwards Java : " + readOnlySet);
//Set is non yet read-only you lot tin nonetheless add together elements into Set
readOnlySet.add("Override");
System.out.println("Set earlier making read alone : " + readOnlySet);
//making Set readonly inwards Java - no add together take or laid functioning permitted
readOnlySet = Collections.unmodifiableSet(readOnlySet);
//trying to add together chemical ingredient inwards read alone Set - java.lang.UnSupportedOperationException
readOnlySet.add("You tin non add together chemical ingredient inwards read Only Set");
//trying to take chemical ingredient from read alone set
readOnlySet.remove("Example"); //you tin non take elements from read alone Set
// Creating read alone Map inwards Java
// Similar to List as well as Set you lot tin besides create read alone or unmodifiable Map inwards Java
// add together , take as well as override is non allowed on read alone Map inwards Java
Map<String, String> contries = new HashMap<String, String>();
contries.put("India", "New Delhi");
//Map is non read alone yet, you lot tin nonetheless add together entries into
contries.put("UK", "London");
System.out.println("Map inwards Java earlier making read only: " + contries);
//Making Map read alone inwards Java
Map readOnlyMap = Collections.unmodifiableMap(contries);
//you tin non lay a novel entry inwards read alone Map inwards Java
readOnlyMap.put("USA", "Washington"); //java.lang.UnSupportedOperation
//you tin non take keys from read alone Map inwards Java
readOnlyMap.remove("UK"); //java.lang.UnSupportedOperation
}
}
That’s all on how to create read alone List, Set as well as Map inwards Java. Its rattling slow to brand whatsoever List implementation read alone past times wrapping it amongst Collections.unModifiableList(). Use same technique to convert whatsoever other Collection into read alone inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt ArrayList as well as Vector inwards Java