Tag Archives: Immutable

[Solved] Exception UnsupportedOperationException Example

java.lang.UnsupportedOperationException is RuntimeException and Unchecked Exception which expected to thrown by JVM(Java Virtual Machine) when try to perform an “optional operation” which is not allowed an object .

The Java framework contains plenty of these, especially in the Collections framework. For example “add” is an optional operation, because immutable collections should not allow it. Throwing UnsupportedOperationException is exactly what you should do if you don’t want to write one of these methods.

Constructors :

  • UnsupportedOprationException() : Constructs an UnsupportedOperationException with no detail message.
  • UnsupportedOprationException(String message) : Constructs an UnsupportedOperationException with specified detail message.
  • UnsupportedOprationException(String message, Throwable cause) : Constructs an UnsupportedOperationException with specified detail message and cause.
  • UnsupportedOprationException(Throwable cause) : Constructs a new exception with the  specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

Example :

In below example problem is List returned from Arrays.AsList() is not like java.util.ArrayList. Arrays.asList() returns a java.util.Arrays$ArrayList which is an immutable list. You can not add or remove any element from this list otherwise will get java.lang.UnsupportedOperationException Exception.

package exceptionhandeling;

import java.util.Arrays;
import java.util.List;

public class UnsupportedOperationException {

	public static void main(String[] args) {
		String [] empArr={"Saurabh","Gaurav","Shailesh","Ankur","Ranjith","Ramesh"};
		//Convert Array to LIst
		List empList=Arrays.asList(empArr);
		/**
		 * By the time you get to the remove(i) statement, list is no longer a java.util.ArrayList.
		 * When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.

		 * Not every List implementation support add method because Arrays.asList() returned
		 * immutable list of fixed size
		 */
		for(String emp:empList)
		{
			empList.add("Sachin");
			System.out.println(emp);
		}

	}

}

Output:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at example.UnsupportedOperationExceptionExample.main(UnsupportedOperationExceptionExample.java:21)

Advertisements

How to convert ArrayList to Array and Array to ArrayList?

Arrays class provide method Arrays.asList()  which convert array type elements to a unmodified fixed size ArrayList.

Same way List provide method as List.toArray(List) which convert ArrayList elements to Array.

Note : List returned from Arrays.AsList() is not like java.util.ArrayList. It
returns a java.util.Arrays$ArrayList which is an immutable list. You can not add or remove any element from this list otherwise will get java.lang.UnsupportedOperationException

Example : 

In below example used method Arrays.asList()  to convert  String array to ArrayList of String elements. On other  side use List.toArray() method to convert ArrayList elements to Arrays of type String elements.

package exceptionhandeling;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayToArrayListConversion {

	public static void main(String[] args) {
		String [] empArr={"Saurabh","Gaurav","Shailesh","Ankur","Ranjith","Ramesh"};
		//Convert Array to LIst
		List empList=Arrays.asList(empArr);
		System.out.println("*****Print ArrayList*****");
		printArrayList(empList);
		//Convert ArrayList to Array
		 empArr=(String[])empList.toArray();
		 System.out.println("*****Print Array*****");
		 printArray(empArr);
	}
	private static void printArrayList(List empList)
	{
		for(String emp:empList)
		{
			System.out.println(emp);
		}
	}
	private static void printArray(String[] empArr)
	{
		for(String emp:empArr)
		{
			System.out.println(emp);
		}
	}
}

Output:

*****Print ArrayList*****
Saurabh
Gaurav
Shailesh
Ankur
Ranjith
Ramesh
*****Print Array*****
Saurabh
Gaurav
Shailesh
Ankur
Ranjith
Ramesh

Java, How to convert List to Arrays and Array to ArrayList

In our day to day programming sometime use this conversion by using iteration but there is another way by using Java API’s as below.

ArrayList to Arrays

List provide toArray() method to convert list elements in Arrays.

List<T> list = new ArrayList<T>();
T[]arr=list.toArray(new T[list.size]);

Example :

import java.util.ArrayList;
import java.util.List;

public class ConvertArrayListToArray {

	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("Georgia");
		list.add("Texas");
		list.add("Newyork");
		list.add("Okhlama");
		String [] states = list.toArray(new String[list.size()]);
	}

}

Arrays to ArrayList

Arrays class provide to asList() method to convert Array to immutable List So if you need to modify anything in List you will get java.lang.UnsupportedOperationException.

If you want to modification after convert to List then create with new ArrayList as below.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConvertArrayToArrayList {

	public static void main(String[] args) {
		String [] states= {"Georgia","Texas","New York", "Okhlama"};
		//Convert Array to immutable List
         List<String> stateList= Arrays.asList(states);
         try
         {
         stateList.add("North Carolina");
         }
         catch(UnsupportedOperationException ex)
         {
        	 ex.printStackTrace();
         }

         //Convert Array to mutable List
         stateList=new ArrayList<String>(Arrays.asList(states));
         stateList.add("North Carolina");
         System.out.println(stateList.get(4));
	}

}

In above example I have considered both cases for Immutable and Mutable.

More Sample Code

For more java and JDBC codes follow below links