Are arrays more serializable than an ArrayList?

Some time ago, our architect gave me this framework, and I could not talk to him more to find out more at that time, but I could not understand how arrays are more serializable / more efficient than ArrayList .

Update : this is in the web services code, if important, and it may happen that it can mean performance instead of serializability.

Update : No XML serialization issues for ArrayList s.

  <sample-array-list>reddy1</sample-array-list> <sample-array-list>reddy2</sample-array-list> <sample-array-list>reddy3</sample-array-list> 

Could there be a problem in a distributed application?

+3
source share
7 answers

There is no such thing as more serializable. Either the class is serializable or not. Both arrays and ArrayList are serializable.

In terms of performance, this is a completely different topic. Arrays, especially primitives, use a little less memory than ArrayLists, but the serialization format is actually equally compact for both.

After all, the only person who can truly explain this vague and misleading expression is the one who created it. I suggest you ask your architect what exactly he had in mind.

+19
source

I assume that you are talking about serializing Java objects.

It turns out that the array (of objects) and ArrayList have similar but not identical contents. In the case of an array, serialization will consist of the object header, the length of the array, and its elements. In the case of ArrayList, serialization consists of the size of the list, the length of the array, and the first elements of the size of the array. Thus, one additional 32-bit int is serialized. There may also be differences in the titles of the respective objects.

So yes, there is a small (probably 4 bytes) difference in the size of the serial views. And it's possible that the array can be serialized / deserialized a little faster. But the differences are likely to be in noise, and don't worry about ... if profiling, etc. It does not mean that this is a bottleneck.

EDIT

Based on @Tom Hawtin comment, object header difference significant, especially if serialization contains only a small number of instances of ArrayList.

+8
source

Perhaps he referred to the XML serialization used by Webservices? Taking advantage of this a few years ago, I remember that the web service returning the List object was hard to connect to (at least I couldn't figure it out, perhaps due to the internal structure of ArrayList and LinkedList s), although it was trivially done when the array was returned.

To send a comment to Reddy,

But in any case (array or ArrayList) is converted to XML, right?

Yes, they will, but XML serialization basically translates into XML all the data contained in the serialized object.

For an array that is a series of values. For example, if you declare and serialize

 int[] array = {42, 83}; 

You will probably get the XML result:

  <array>42</array> <array>83</array 

For ArrayList , i.e .:

  • an array (obviously) that may be larger than the actual number of elements
  • several other members, such as integer indices ( firstIndex and lastIndex ), counts, etc.

(you can find it all in source for ArrayList.java )

Thus, all of them will be converted to XML, which makes it difficult for the Webservice client to read the actual values: it must read the index values, find the actual array and read the values ​​contained between the two indexes.

Serialization:

 ArrayList<Integer> list = new ArrayList<Integer>(); list.add(42); list.add(83); 

might look like this:

 <firstIndex>0</firstIndex> <lastIndex>2</lastIndex> <E>42</E> <E>83</E> <E>0</E> <E>0</E> <E>0</E> <E>0</E> <E>0</E> <E>0</E> <E>0</E> <E>0</E> 

Thus, when using XML serialization in Webservices, it is better to use arrays (for example, int[] ) than collections (for example, ArrayList<Integer> ). To do this, it may be useful to convert collections to arrays using Collection#toArray() .

+4
source

They both serialize the same data. Therefore, I would not say that one of them is much better than the other.

+2
source

As I know, both are Serializable, but using arrays is best because the main goal of implementing ArrayList is the internal task of simple manipulation, and not revealing the outside world. It is a little harder to use, so when used in webservices when serialized, it can cause problems in the namespace and headers. If it automatically installs them, you will not be able to correctly receive or send data. Therefore, it is better to use primitive arrays.

+1
source

Only in Java does it matter, and even then it is hard to notice.

If he did not mean Java, then yes, your best option is most likely to ask him what exactly he had in mind.

0
source

Just a related thought: the List interface is not Serializable, so if you want to include List in the Serializable API, you will either have to set up a Serializable implementation like ArrayList, or convert List to an array. Good design methods prevent the disclosure of your implementation, and therefore your architect encourages you to convert the list to an array. You pay a little time turning List into an array, but on the other end you can wrap the array with a list interface using java.util.Arrays.asList (), which is fast.

0
source

All Articles