Bridge between Array and Collection APIs

1. Overview

Generally speaking, container is an object such as box which can be used to hold something. In programming container means the same thing. It is used to store the data elements. Arrays and Collections are containers. They have their own properties like Arrays provide indexed way to access an element whereas a hash based collection will provide a hash function to access an element.

This article is focused on 3 different methods that acts as bridge between Array and Collection APIs i.e. converting a Collection to an Array and vice-versa.

Embed from Getty Images

2. Converting Collection to Array

First, let us explore 2 different ways through which we will convert a Collection i.e. any type of Collection to an array.

2.1. Object[] toArray()

Object[] toArray() method is defined in Collection interface and hence this method must be implemented by all class who implement this interface. This method converts the given collection to Object[]. The interface provides a guarantee that the returned Object[] is will be safe which means that the implementing class will copy all the elements to the new array and return the new array hence no reference are maintained by the collection. So clients are allowed to edit the Object[] array. Remember the order in which elements are returned depends on implementation of iterator of a Collection. Example, ArrayList will return elements in proper sequence i.e. from first to last whereas HashSet will return elements based on hash function.

The method is incredibly simple to use. We should all thank Joshua Bloch who designed such simple yet powerful things.

List<String> names = new ArrayList<>();
names.add("Cersei");
names.add("Jaime");
names.add("Tyrion");
Object[] objArrNames = names.toArray();

2.2. <T> T[] toArray(T[] a)

This method is specialized version of previous toArray() method. The type of returned array is specified in method’s parameter. Apart from that this method is same as Object[] toArray() method.

The collection will try to copy the elements into T[] if the size of T[] array is less than size of collection. If not, it will create new T[] and copy elements into it. It is actually better to use this method rather than Object[] toArray() method as we get an array with proper type.

List<String> names = new ArrayList<>();
names.add("Cersei");
names.add("Jaime");
names.add("Tyrion");
String[] objArrNames = names.toArray(new String[0]);

3. Converting Array to Collection

The second phase of this article is to convert an array to a collection.

3.1 Arrays.asList(T… a)

asList(T… a) method is a static factory in class Arrays that allows arrays to be viewed as List. Needless to say this method acts as bridge between Array and Collection APIs. The asList method accepts varargs or an array as a parameter and returns List. Remember the elements from array are not copied to List. Instead it is just referenced. Hence changes in array will reflect changes in List.

String[] lannister = {"Cersei", "Jaime", "Tyrion"};
List<String> list = Arrays.asList(lannister);

String[] lannister = {"Cersei", "Jaime", "Tyrion"};
List<String> list = Arrays.asList(lannister); 
System.out.println(list); // [Cersei, Jaime, Tyrion]

lannister[0] = "aaa";
System.out.println(list); // [aaa, Jaime, Tyrion]

4. Conclusion

In this article we saw 3 different methods that acts as bridge between Arrays and Collections. I believe we should use Collections in general over Arrays. They are generic in nature and provide far better functionality than Arrays. There can be argument that arrays uses less storage than Collection, while that may be true but remember you get much more functionality than Arrays. Consider Collection as a better container than Arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *