Home  >  Core Java

Java Unmodifiable SortedMap and SortedSet Example

By Arvind Rai, January 25, 2020
On this page we will provide Java unmodifiable SortedMap and SortedSet example. Java Collections have some static methods that return unmodifiable view for the specified object and the returned instance will be read-only and if we try to modify, it will throw UnsupportedOperationException. Find these static methods.
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 unmodifiableSortedMap(), unmodifiableSortedSet() and unmodifiableCollection() methods.

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 

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 
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us