This example shows the possible causes of java.lang.UnsupportedOperationException. This example also shows how to fix or resolve java.lang.UnsupportedOperationException.
What is java.lang.UnsupportedOperationException?
java.lang.UnsupportedOperationException is thrown to denote that the requested operation is not supported by the underlying collection object.
Possible causes and resolutions of the java.lang.UnsupportedOperationException
1) Trying to add or remove elements from the unmodifiable list object
The List object returned by the asList
method of the Arrays class is unmodifiable. That is, you cannot change the list object structurally once it is created. Trying to add or remove elements from such a list will throw the UnsupportedOperationException exception.
The Arrays#List
creates a wrapper around the original array and creates a fixed-size list that cannot be modified. Thus, add and remove operations are not supported on such List object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
String[] strArray = {"Sun", "Mon", "Tue"}; //fixed size unmodifiable list List list = Arrays.asList(strArray); try{ //add operation is not supported list.add("Wed"); }catch(UnsupportedOperationException uoe){ System.out.println("Add operation is not supported"); uoe.printStackTrace(); } try{ //remove operation is not supported list.remove(0); }catch(UnsupportedOperationException uoe){ System.out.println("Remove operation is not supported"); uoe.printStackTrace(); } |
Output
1 2 3 4 5 6 7 8 9 |
Add operation is not supported java.lang.UnsupportedOperationException at java.util.AbstractList.add(Unknown Source) at java.util.AbstractList.add(Unknown Source) at com.javacodeexamples.exceptionexamples.UnsupportedExceptionExample.main(UnsupportedExceptionExample.java:16) Remove operation is not supported java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source) at com.javacodeexamples.exceptionexamples.UnsupportedExceptionExample.main(UnsupportedExceptionExample.java:25) |
Fix/Resolution
Convert list object returned by the asList
method to an ArrayList before adding or removing elements from it like given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
String[] strArray = {"Sun", "Mon", "Tue"}; //fixed-size unmodifiable list List list = Arrays.asList(strArray); //convert fixed size list to an ArrayList list = new ArrayList(list); list.add("Wed"); list.remove(0); System.out.println("List is modified"); |
2) Trying to remove elements using an Iterator
Theremove
method of an Iterator class may throw the UnsupportedOperationException if the iterator is obtained from an unmodifiable List object (like given in the above example) and the remove
method is called while iterating over the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
String[] strArray = {"Sun", "Mon", "Tue"}; //fixed size unmodifiable list List list = Arrays.asList(strArray); Iterator itrList = list.iterator(); while(itrList.hasNext()){ if( itrList.next().equals("Mon") ){ //remove operation is not supported itrList.remove(); } } |
Output
1 2 3 4 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source) at java.util.AbstractList$Itr.remove(Unknown Source) at com.javacodeexamples.exceptionexamples.UnsupportedExceptionExample.main(UnsupportedExceptionExample.java:50) |
Fix/Resolution
Create a new ArrayList or LinkedList object from the list before iterating and removing elements from the Iterator or List.
1 2 3 4 5 6 7 8 9 10 11 |
//create new ArrayList from fixed size list object list = new ArrayList<String>(list); Iterator<String> itrList = list.iterator(); while(itrList.hasNext()){ if( itrList.next().equals("Mon") ){ itrList.remove(); } } |
3) Trying to add, remove or set elements using ListIterator
Theadd
, set
, andremove
methods of the ListIterator may throw UnsupportedOperationException if the ListIterator is obtained from a fixed-size List object and any of these methods are called while iterating over such a list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
String[] strArray = {"Sun", "Mon", "Tue"}; //fix sized unmodifiable list List list = Arrays.asList(strArray); ListIterator listItr = list.listIterator(); if(listItr.hasNext()){ listItr.next(); //set is not supported listItr.set("Sat"); //add is not supported listItr.add("Wed"); //Remove is not supported listItr.remove(); } |
Fix/Resolution
Create a new ArrayList object from the fixed size list and get the ListIterator from ArrayList instead of a fixed size list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
String[] strArray = {"Sun", "Mon", "Tue"}; //fix sized unmodifiable list List list = Arrays.asList(strArray); //create new ArrayList from fixed size list object list = new ArrayList(list); ListIterator listItr = list.listIterator(); if(listItr.hasNext()){ listItr.next(); listItr.set("Sat"); listItr.remove(); } |
4) Trying to use add or addAll methods on views
Some methods of various collection classes return a view that is backed by the original collection and does not support add
or addAll
operations.
One such example is the keySet
method of the HashMap class. It returns a Set view of all the keys of the map that does not support add
and addAll
operations. If you try to call any of these methods on such a view, the code throws UnsupportedOperationException exception as given below.
1 2 3 4 5 6 7 8 9 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); //get the key set Set<Integer> keys = hMapNumbers.keySet(); keys.add(4); |
Output
1 2 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractCollection.add(Unknown Source) |
Fix/Resolution
Add element/mappings to the original collection object instead of the view. Since it is just a view, the changes will be reflected in the view if you change the original collection object.
Please let me know your views in the comments section below.
Thanks i have struggled so long not knowing this about unmodifiable – lists!
Hi Dan,
Glad I could help. Thanks.