Convert array to list

In contrast to converting an list to an array, this example will show how to convert a primitive and object array to an arraylist using java, java 8, guava, apache libraries and spring framework.

Straight up Java

Object array - Arrays.asList

Using core java's Arrays class, a class containing methods for manipulating arrays, Arrays.asList will convert an array to a list of the same size.

@Test
public void convert_string_array_to_list_with_java () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = Arrays.asList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

Object array - Collections.addAll

Collections.addAll will add all of the elements of an array to a specified collection which is similar behavior to above using Arrays.asList.

@Test
public void convert_string_array_to_list_with_java_using_collections () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = new ArrayList<String>();
    Collections.addAll(planetsAsArrayList, planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

Primitive - Loop

Using a standard java technique, we will iterate over a primitive array using a for loop. Each iteration will add a specified value from the array to the list using autoboxing. Autoboxing is the automatic conversion between primitive types and their corresponding object wrapper classes. Be cautious while using autoboxing as it has performance implications.

@Test
public void convert_primitive_array_to_list_with_java () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = new ArrayList<Integer>(someNotSoRandomAscii.length);
    for (int x = 0; x < someNotSoRandomAscii.length; x++) {
        someNotSoRandomAsciiAsList.add(someNotSoRandomAscii[x]);
    }
    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Google Guava

Object array

Guava Lists utility class can be used when needing to convert an object array to list simply by calling the Lists.newArrayList and passing in the specified array.

@Test
public void convert_string_array_to_list_with_guava () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = Lists
            .newArrayList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

Primitive array

Guava Ints utility class can be used to convert a primitive array of ints to a list. Similar to Arrays.asList method above, Ints.asList will return a fixed size list which will be backed by the specified array.

@Test
public void convert_primitive_array_to_list_with_guava () {
    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = Ints.asList(someNotSoRandomAscii);

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Apache Commons

Object array

Converting an object array with apache commons can be easily done by using CollectionUtils.addAll method which will add all the elements of an array to the specified collection.

@Test
public void convert_string_array_to_list_with_apachecommons () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = new ArrayList<String>();
    CollectionUtils.addAll(planetsAsArrayList, planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

Primitive array

When converting a primitive array using apache commons we first must use ArrayUtils to convert primitive values to objects. Once this operation is performed we will call the Arrays.asList to transform the array to a List of integers.

@Test
public void convert_primitive_array_to_list_with_apachecommons () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = Arrays.asList(ArrayUtils.toObject(someNotSoRandomAscii));

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Spring Framework

If you are already dependent on spring framework, an option would be to use org.springframework.util.CollectionUtils.arrayToList which will convert the supplied array into a list while wrapping primitive arrays with the appropriate wrapper type. One thing to be cautious of is that the java doc states that this class is mainly for internal use within the framework.

Object array

@Test
public void convert_string_array_to_list_with_spring () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    @SuppressWarnings("unchecked")
    List<String> planetsAsArrayList = org.springframework.util
            .CollectionUtils.arrayToList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

Primitive array

@Test
public void convert_primitive_array_to_list_with_spring () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    @SuppressWarnings("unchecked")
    List<Integer> someNotSoRandomAsciiAsList = org.springframework.util
            .CollectionUtils.arrayToList(someNotSoRandomAscii);

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}