Add to Google Reader or Homepage

How to resolve UnsupportedOperationException:


 UnsupportedOperationException:

This is One of the most commonly experienced exception by the java programmers who deals with the immutable or unmodifiable objects.In this post lets see how to deal with this exception.Before that let us absorb what exception is this?when it will be thrown? and finally how to fix this exception.

What is meant by UnsupportedOperationException?

The UnsupportedOperationException tells us that a particular operation cannot be performed  as it was restricted for use against a particular object.This exception extends the Runtimeexception which serves as a  super class for a group of run-time exceptions.

When this UnsupportedOperationException is thrown?

As the name implies that this exception is thrown when the requested operation cannot be supported. As i have said before this exception is thrown by almost all of the Concrete collections like.
1.Lists
2.Queue
3.Set
4.Maps.

Let us have a quick overview of java collection frameworks related to our discussion.

All those collections that i have listed above are used to store some values and provides some means to traverse the values and even allows the modification of collection.We can create views for those Collections except queue.

You may wonder what is a view?.A view is a read-only format of the collections,which means that through view we can  traverse the collections and even we can retrieve values.But if you try to modify the collection using view object  this will cause an UnsupportedOperationException to be thrown.For creating Unmodifiable views java provides the following methods.

1.Collections.unmodifiableCollection.
2.Collections.unmodifiableList
3.Collections.unmodifiableSet
4.Collections.unmodifiableSortedSet
5.Collections.unmodifiableMap
6.Collections.unmodifiableSortedMap.

Note:

1.The Map collections allow the use of remove() method but does not support add() method using the view object.
2.The un modifiable views does not support either add() or remove() method.


An another important cause of this exception is the use of wrappers between the collections and the primitive types.

How to overcome UnsupportedOperationException?

As we know the cause of this exception it will be quite easy to deal with it.Since view objects do not allow
modification of the collection,the solution that i suggest you is to use the object of the collection rather than using the view object for modification.

Examples for the UnsupportedOperationException:


Use of Wrappers:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
String s[]={"ram","ganesh","paul"};
List li=Arrays.asList(s);
li.clear();
System.out.println(" The values in the list: "+li);
}
}

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractList.remove(Unknown Source)
        at java.util.AbstractList$Itr.remove(Unknown Source)
        at java.util.AbstractList.removeRange(Unknown Source)
        at java.util.AbstractList.clear(Unknown Source)
        at Unsupport.main(Unsupport.java:9)

KeySet View of Map collections:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
Map s=new HashMap();
s.put("1","Ram");
s.put("2","Ganesh");
s.put("3","Paul");
System.out.println(s);
Set keys = s.keySet();
keys.add("7");
System.out.println(s);
}
}

{3=Paul, 2=Ganesh, 1=Ram}
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractCollection.add(Unknown Source)
        at Unsupport.main(Unsupport.java:12)

Unmodifiable view of Map collections:

import java.util.*;
public class Unsupport
{
public static void main(String args[])
{
Map s=new HashMap();
s.put("1","Ram");
s.put("2","Ganesh");
s.put("3","Paul");
System.out.println(s);
Map m=Collections.unmodifiableMap(s);
m.put("7","raj");
}
}

{3=Paul, 2=Ganesh, 1=Ram}
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableMap.put(Unknown Source)
        at Unsupport.main(Unsupport.java:12)





9 comments:

brijesh said...

Exacly what i was looking for

pinky shah said...

helped me solve my problem.. thanks a ton

amit doshi said...

What was the solution man. I am not able to get solution from this post.

Md. Kaderi Kibria said...

Nice description. Thanks

Ash Sharma said...

I am getting this error during installation of latest JDK on 64 bit Windows 10 OS. Can someone please help to fix this annoying and un-ubiqutous problem.. cheers

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Saradha Devi said...

informative one thanks for sharing

Selenium Training | Selenium Training Institute in Chennai | Best Selenium Training Institutes in Chennai | Software Testing Training in Chennai

Geetha Devi said...

Good post..Android Training in Chennai | Best Android Training in Chennai | Best Android Training in Chennai with Placement

Saradha Devi said...

great article keep updating

Selenium Training | Selenium Training Institute in Chennai | Best Selenium Training Institutes in Chennai | Software Testing Training in Chennai

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top