In earlier article, we discussed the difference between HashSet and TreeSet and also covered sample example of TreeSet and HashSet. In the current article, we will see one of important exception in collection i.e UnsupportedOperationException.
What is UnsupportedOperationException in collection?
The name itself suggests provided operation can not be supported i.e when you try to perform some operation on collection where it's not allowed and will throw UnsupportedOperationException. The UnsupportedOperationException is thrown at run time so it's RuntimeException i.e Unchecked Exception.
Throwable -> Exception -> RuntimeException -> UnsupportedOperationException
Scenario to throw UnsupportedOperationException:-
1) Update on list of Arrays.asList()
As we know that, Converting from Array to List we will use asList() method of Arrays(java.util.Arrays) class. When you convert array to list using asList method, and try to add an element to a list then will throw UnsupportedOperationException as shown below code.
UnsupportedOpExceptionExample.java,
Output:-
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at com.example.demo.UnsupportedOpExceptionExample.main(UnsupportedOpExceptionExample.java:12)
Solution:-
Pass Arrays.asList to the List constructor, so that will act as new ArrayList object, then you can modify the list without any exception.
UnsupportedOpExceptionExample.java
Output:-
Mechanical
Computer Science
ECE
Output:-
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at com.example.demo.UnsupportedOpExceptionExample.main(UnsupportedOpExceptionExample.java:16)
3) How to iterate the TreeMap in reverse order in Java
4) How to Remove duplicates from ArrayList in Java
5) Internal Implementation of TreeMap in Java
6) Internal implementation of ArrayList in Java
7) Internal Implementation of LinkedList in Java
8) Difference between HashSet and TreeSet in Java
What is UnsupportedOperationException in collection?
The name itself suggests provided operation can not be supported i.e when you try to perform some operation on collection where it's not allowed and will throw UnsupportedOperationException. The UnsupportedOperationException is thrown at run time so it's RuntimeException i.e Unchecked Exception.
Throwable -> Exception -> RuntimeException -> UnsupportedOperationException
Scenario to throw UnsupportedOperationException:-
1) Update on list of Arrays.asList()
As we know that, Converting from Array to List we will use asList() method of Arrays(java.util.Arrays) class. When you convert array to list using asList method, and try to add an element to a list then will throw UnsupportedOperationException as shown below code.
UnsupportedOpExceptionExample.java,
package com.example.demo; import java.util.Arrays; import java.util.List; public class UnsupportedOpExceptionExample { public static void main(String[] args) { String[] arrayStr = { "Mechanical", "Computer Science" }; List<String> list = Arrays.asList(arrayStr); list.add("ECE"); } }
Output:-
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at com.example.demo.UnsupportedOpExceptionExample.main(UnsupportedOpExceptionExample.java:12)
Solution:-
Pass Arrays.asList to the List constructor, so that will act as new ArrayList object, then you can modify the list without any exception.
UnsupportedOpExceptionExample.java
package com.example.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class UnsupportedOpExceptionExample { public static void main(String[] args) { String[] arrayStr = { "Mechanical", "Computer Science" }; List<String> list = new ArrayList<String>(Arrays.asList(arrayStr)); list.add("ECE"); list.stream().forEach(str -> System.out.println(str)); } }
Mechanical
Computer Science
ECE
2) Update on Unmodifiable collection
You can create any Unmodifiable collection class by using Collections.unmodifiableCollection (Collection obj) method. When you create unmodifiable collection, you can not modify i.e it's read only access. If you try to update on Unmodifiable collection, it will throw RuntimeException i.e UnsupportedOperationException.
Sample java code to throw UnsupportedOperationException when try to update Unmodifiable collection,
UnsupportedOpExceptionExample.java
You can create any Unmodifiable collection class by using Collections.unmodifiableCollection (Collection obj) method. When you create unmodifiable collection, you can not modify i.e it's read only access. If you try to update on Unmodifiable collection, it will throw RuntimeException i.e UnsupportedOperationException.
Sample java code to throw UnsupportedOperationException when try to update Unmodifiable collection,
UnsupportedOpExceptionExample.java
package com.example.demo; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; public class UnsupportedOpExceptionExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Mechanical"); list.add("ECE"); Collection<String> strList = Collections.unmodifiableCollection(list); strList.add("CS"); list.stream().forEach(str -> System.out.println(str)); } }
Output:-
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at com.example.demo.UnsupportedOpExceptionExample.main(UnsupportedOpExceptionExample.java:16)
If really you are not doing any update on collection then use this method, otherwise don't use.
Thank you for visiting blog....
Related Posts:--
2) Collection Interview Questions and Answers in Java(List,Map & Set)3) How to iterate the TreeMap in reverse order in Java
4) How to Remove duplicates from ArrayList in Java
5) Internal Implementation of TreeMap in Java
6) Internal implementation of ArrayList in Java
7) Internal Implementation of LinkedList in Java
8) Difference between HashSet and TreeSet in Java
No comments:
Post a Comment