Home  >  Core Java

Java Collections Unmodifiable List, Map, Set, SortedMap and SortedSet Example

By Arvind Rai, July 10, 2016
On this page we will provide java Collection unmodifiable List, Map, Set, SortedMap and SortedSet example. Java Collections have static methods that return unmodifiable view for the specified object and then the returned instance will be read-only and if we try to modify, it will throw UnsupportedOperationException.
unmodifiableCollection(): We pass any collection instance and the method returns unmodifiable view for the specified collection object.
unmodifiableList(): It returns an unmodifiable List.
unmodifiableMap(): It returns an unmodifiable Map.
unmodifiableSet(): It returns an unmodifiable Set.
unmodifiableSortedMap(): It returns an unmodifiable SortedMap.
unmodifiableSortedSet(): It returns an unmodifiable SortedSet().

Find the example for the above methods.

Collections.unmodifiableCollection

UnmodifiableCollectionDemo.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class UnmodifiableCollectionDemo {
    public static void main(String[] args) {
    	Collection<String> modifiable = new ArrayList<>();
    	modifiable.add("1111");
    	Collection<String> unmodifiable = Collections.unmodifiableCollection(modifiable);
    	try {
    		unmodifiable.add("2222"); //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	for(String s : unmodifiable){
    		System.out.println(s);
    	}
    }
} 
Output
UnsupportedOperationException-----
1111 



Collections.unmodifiableList

UnmodifiableListDemo.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UnmodifiableListDemo {
    public static void main(String[] args) {
    	List<String> modifiable = new ArrayList<>();
    	modifiable.add("1111");
    	List<String> unmodifiable = Collections.unmodifiableList(modifiable);
    	try {
    		unmodifiable.add("2222"); //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	for(String s : unmodifiable){
    		System.out.println(s);
    	}
    }
} 
Output
UnsupportedOperationException-----
1111 

Collections.unmodifiableMap

UnmodifiableMapDemo.java
package com.concretepage.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class UnmodifiableMapDemo {
    public static void main(String[] args) {
    	Map<Integer, String> modifiable = new HashMap<>();
    	modifiable.put(1, "AAAA");
    	Map<Integer, String> unmodifiable = Collections.unmodifiableMap(modifiable);
    	try {
    		unmodifiable.put(2, "BBBB");; //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	Set<Integer> set = unmodifiable.keySet();
    	Iterator<Integer> ite = set.iterator();
    	while(ite.hasNext()){
    		System.out.println(unmodifiable.get(ite.next()));
    	}
    }
} 
Output
UnsupportedOperationException-----
AAAA 

Collections.unmodifiableSet

UnmodifiableSetDemo.java
package com.concretepage.util;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class UnmodifiableSetDemo {
    public static void main(String[] args) {
    	Set<String> modifiable = new HashSet<>();
    	modifiable.add("1111");
    	Set<String> unmodifiable = Collections.unmodifiableSet(modifiable);
    	try {
    		unmodifiable.add("2222"); //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	for(String s : unmodifiable){
    		System.out.println(s);
    	}
    }
} 
Output
UnsupportedOperationException-----
1111 

Collections.unmodifiableSortedMap

UnmodifiableSortedMapDemo.java
package com.concretepage.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class UnmodifiableSortedMapDemo {
    public static void main(String[] args) {
    	SortedMap<Integer, String> modifiable = new TreeMap<>();
    	modifiable.put(1, "AAAA");
    	SortedMap<Integer, String> unmodifiable = Collections.unmodifiableSortedMap(modifiable);
    	try {
    		unmodifiable.put(2, "BBBB");; //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	Set<Integer> set = unmodifiable.keySet();
    	Iterator<Integer> ite = set.iterator();
    	while(ite.hasNext()){
    		System.out.println(unmodifiable.get(ite.next()));
    	}
    }
} 
Output
UnsupportedOperationException-----
AAAA 

Collections.unmodifiableSortedSet

UnmodifiableSortedSetDemo.java
package com.concretepage.util;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
public class UnmodifiableSortedSetDemo {
    public static void main(String[] args) {
    	SortedSet<String> modifiable = new TreeSet<>();
    	modifiable.add("1111");
    	SortedSet<String> unmodifiable = Collections.unmodifiableSortedSet(modifiable);
    	try {
    		unmodifiable.add("2222"); //will throw error
    	}catch(UnsupportedOperationException e) {
    		System.out.println("UnsupportedOperationException-----");
    	}
    	for(String s : unmodifiable){
    		System.out.println(s);
    	}
    }
} 
Output
UnsupportedOperationException-----
1111 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us