Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolve a java.util.ArrayList$SubList notSerializable Exception

I am using SubList function on an object of type List. The problem is that I am using RMI and because the java.util.ArrayList$SubList is implemented by a non-serializable class I got the Exception described above when I try to pass the resulting object to a remote function taking as an argument a List as well. I've seen that I should copy the resulting List to a new LinkedList or ArrayList and pass that.

Does anyone know a function that helps as to easily do that for this for example ?

List<String> list = originalList.subList(0, 10); 
like image 939
Othmane Avatar asked Oct 25 '14 23:10

Othmane


People also ask

What is the way to get subList from an ArrayList in Java?

The subList() method of java. util. ArrayList class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.)

What is subList method in Java?

The Java ArrayList subList() method extracts a portion of the arraylist and returns it. The syntax of the subList() method is: arraylist.subList(int fromIndex, int toIndex) Here, arraylist is an object of the ArrayList class.


1 Answers

It's because, List returned by subList() method is an instance of 'RandomAccessSubList' which is not serializable. Therefore you need to create a new ArrayList object from the list returned by the subList().

ArrayList<String> list = new ArrayList<String>(originalList.subList(0, 10)); 
like image 127
aravindaM Avatar answered Sep 22 '22 21:09

aravindaM