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)





16 comments:

brijesh said...

Exacly what i was looking for

Unknown said...

helped me solve my problem.. thanks a ton

Unknown said...

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

Md. Kaderi Kibria said...

Nice description. Thanks

Unknown 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

Unknown said...

informative one thanks for sharing

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

Unknown said...

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

Unknown said...

great article keep updating

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

priya said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Anonymous said...

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
Mobile service center in chennai | iphone service center in chennai | ipod battery replacement in chennai | ipad display replacement in chennai | 100% genuine mobile parts | Mobile phone display replacement in chennai | Mobile display replacement in chennai | Mobile service center in chennai | Mobile display replacement in chennai | Tablet battery replacement service in chennai | Latop service center in chennai | Mobile service center in chennai

Kamila said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Dot Net Coaching Centers in Chennai
Manual Testing Training in Chennai
Core Java Training in Chennai
Best PHP Course in Chennai

Nisha San said...

Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

Keerthi55 said...

python training
angular js training
selenium trainings
sql server dba training

Ava Volkov said...

Your ability to give a balanced viewpoint that takes into account other viewpoints and addresses potential problems stands out above all else. Splunk Certification

Ava Volkov said...

You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. DevOps Training

Ava Volkov said...

You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. DevOps Certification

Post a Comment

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