Solve java.lang.UnsupportedOperationException in java

What is hierarchy of java.lang. UnsupportedOperationException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.UnsupportedOperationException



UnsupportedOperationException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.UnsupportedOperationException is a RuntimeExceptions in java.



What is UnsupportedOperationException in java?

UnsupportedOperationException is thrown when requested operation is not supported. Example - Modifying unmodifiable list either by adding or removing elements throws UnsupportedOperationException in java.



  1. java.util.Arrays.asList method returns fixed size list, i.e. unmodifiable list and Modifying unmodifiable list either by adding or removing elements throws UnsupportedOperationException in java

  2. making list unmodifiable using Collections.unmodifiableList and Modifying unmodifiable list either by adding or removing elements throws java.lang.UnsupportedOperationException in java.

  3. making set unmodifiable using Collections.unmodifiableSet and Modifying unmodifiable set either by adding or removing elements throws java.lang.UnsupportedOperationException in java

  4. making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java



Scenarios where UnsupportedOperationException may be thrown in java>

Program/Example 1 > java.util.Arrays.asList method returns fixed size list, i.e. unmodifiable list and Modifying unmodifiable list either by adding or removing elements throws UnsupportedOperationException in java

package unsupportedOperationExceptionExamplePackage;
import java.util.Arrays;
import java.util.List;
public class UnsupportedOperationExceptionExample {
public static void main(String[] args) {
     String stringArray[] = {"a", "b"};
     List<String> list = Arrays.asList(stringArray);
     System.out.println(list);
     list.add("c"); //It will throw java.lang.UnsupportedOperationException
}
}
/*OUTPUT
[a, b]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at unsupportedOperationExceptionExamplePackage.UnsupportedOperationExceptionExample.main(UnsupportedOperationExceptionExample.java:11)
*/



Program/Example 2 >ArrayList - making list unmodifiable using Collections.unmodifiableList and Modifying unmodifiable list either by adding or removing elements throws java.lang.UnsupportedOperationException in java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class ArrayListTest {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          List<String> arrayList = new ArrayList<String>();
          arrayList.add("ankit");
          arrayList.add("javaMadeSoEasy");
          // getting unmodifiable list
          List<String> unmodifiableList = Collections.unmodifiableList(arrayList);
         
          /*
          * Now any attempt to modify list will throw java.lang.UnsupportedOperationException
          */
          unmodifiableList.add("mittal");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
   at ArrayListTest.main(ArrayListTest.java:24)
*/


Program/Example 3 >LinkedList - making list unmodifiable using Collections.unmodifiableList and Modifying unmodifiable list either by adding or removing elements throws java.lang.UnsupportedOperationException in java


import java.util.LinkedList;
import java.util.Collections;
import java.util.List;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class LinkedListUnModifiableExample {
   public static void main(String args[]) {
          List<String> linkedList = new LinkedList<String>();
          linkedList.add("ankit");
          linkedList.add("javaMadeSoEasy");
          // getting unmodifiable list
          List<String> unmodifiableList = Collections.unmodifiableList(linkedList);
         
          /*
          * Now any attempt to modify list will throw java.lang.UnsupportedOperationException
          */
          unmodifiableList.add("mittal");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
   at LinkedListUnModifiableExample .main(LinkedListTest.java:23)
*/



Program/Example 4 >HashSet - making set unmodifiable using Collections.unmodifiableSet and Modifying unmodifiable set either by adding or removing elements throws java.lang.UnsupportedOperationException in java

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class HashSetTest {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          Set<String> hashSet = new HashSet<String>();
          hashSet.add("ankit");
          hashSet.add("javaMadeSoEasy");
          // getting unmodifiable HashSet
          Set<String> unmodifiableSet = Collections.unmodifiableSet(hashSet);
         
          /*
          * Now any attempt to modify list will throw java.lang.UnsupportedOperationException
          */
          unmodifiableSet.add("mittal");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
   at HashSetTest.main(HashSetTest.java:24)

*/





Program/Example 5 >HashMap - making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java



import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class HashMapTest {
   public static void main(String args[]){
         
          Map<Integer,String> hashMap=new HashMap<Integer,String>();
  
          hashMap.put(11, "ankit");
          hashMap.put(21, "mittal");
          hashMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable HashMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(hashMap);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at HashMapTest.main(hashMapTest.java:24)
*/


Program/Example 6 >LinkedHashSet  - making set unmodifiable using Collections.unmodifiableSet and Modifying unmodifiable set either by adding or removing elements throws java.lang.UnsupportedOperationException in java


import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class LinkedHashSetUnmodifiableExample {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          Set<String> linkedLinkedHashSet = new LinkedHashSet<String>();
          linkedLinkedHashSet.add("ankit");
          linkedLinkedHashSet.add("javaMadeSoEasy");
          // getting unmodifiable LinkedHashSet
          Set<String> unmodifiableSet = Collections.unmodifiableSet(linkedLinkedHashSet);
         
          /*
          * Now any attempt to modify list will throw java.lang.UnsupportedOperationException
          */
          unmodifiableSet.add("mittal");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
   at main(LinkedHashSetUnmodifiableExample.java:24)
*/



Program/Example 7 > Hashtable - making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java



Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes



import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class HashtableTest {
   public static void main(String args[]){
         
          Map<Integer,String> hashtable=new Hashtable<Integer,String>();
  
          hashtable.put(11, "ankit");
          hashtable.put(21, "mittal");
          hashtable.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable Hashtable
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(hashtable);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at hashtableTest.main(hashtableTest.java:24)
*/


Program/Example 8 >

ConcurrentSkipListMap - making map unmodifiable using Collections.unmodifiableMap in java and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java

HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences



import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentSkipListMapUnmodifiableExample {
   public static void main(String args[]){
         
          Map<Integer,String> concurrentSkipListMap=new ConcurrentSkipListMap<Integer,String>();
  
          concurrentSkipListMap.put(11, "ankit");
          concurrentSkipListMap.put(21, "mittal");
          concurrentSkipListMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable ConcurrentSkipListMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(concurrentSkipListMap);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at concurrentSkipListMapTest.main(concurrentSkipListMapTest.java:24)
*/



Program/Example 9 >

TreeMap - making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java

HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences



import java.util.Collections;
import java.util.TreeMap;
import java.util.Map;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class TreeMapUnmodifiableExample {
   public static void main(String args[]){
         
          Map<Integer,String> treeMap=new TreeMap<Integer,String>();
  
          treeMap.put(11, "ankit");
          treeMap.put(21, "mittal");
          treeMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable TreeMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(treeMap);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at treeMapTest.main(treeMapTest.java:24)
*/


Program/Example 10 >

ConcurrentHashMap - making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java



HashMap and ConcurrentHashMap - Similarity and Differences



HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences


import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentHashMapUnmodifiableExample {
   public static void main(String args[]){
         
          Map<Integer,String> concurrentHashMap=new ConcurrentHashMap<Integer,String>();
  
          concurrentHashMap.put(11, "ankit");
          concurrentHashMap.put(21, "mittal");
          concurrentHashMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable ConcurrentHashMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(concurrentHashMap);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at  ConcurrentHashMapUnmodifiableExample.main(ConcurrentHashMapUnmodifiableExample.java:24)
*/


Program/Example 11 >LinkedHashMap - making map unmodifiable using Collections.unmodifiableMap and Modifying unmodifiable map either by adding or removing key-value pair throws java.lang.UnsupportedOperationException in java

HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences



Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes



import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class LinkedHashMapTest {
   public static void main(String args[]){
         
          Map<Integer,String> linkedHashMap=new LinkedHashMap<Integer,String>();
  
          linkedHashMap.put(11, "ankit");
          linkedHashMap.put(21, "mittal");
          linkedHashMap.put(31, "javaMadeSoEasy");
         
          //getting unmodifiable LinkedHashMap
          Map<Integer,String> unmodifiableMap = Collections.unmodifiableMap(linkedHashMap);
  
          /*
          * Now any attempt to modify map will throw java.lang.UnsupportedOperationException
          */
          unmodifiableMap.put(41,"java");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.UnsupportedOperationException
   at java.util.Collections$UnmodifiableMap.put(Unknown Source)
   at linkedHashMapTest.main(linkedHashMapTest.java:24)
*/



How to Avoid or Solve UnsupportedOperationException in java >


Never try to modify list, set or map which has been made unmodifiable.



  • Never modify list returned by  java.util.Arrays.asList method to avoid java.lang.UnsupportedOperationException in java



  • Never modify list which has been made unmodifiable using Collections.unmodifiableList method to avoid java.lang.UnsupportedOperationException in java



  • Never modify set which has been made unmodifiable using Collections.unmodifiableSet method to avoid java.lang.UnsupportedOperationException in java.



  • Never modify map which has been made unmodifiable using Collections.unmodifiableMap method to avoid java.lang.UnsupportedOperationException in java.


eEdit
Must read for you :