Convert Array to List and vice versa in Java

0
420
Convert Array to List and vice versa in Java

Back to the basic of Java core practice, one of the pretty much common tasks that Java developers face everyday is to convert Array to List and vice versa. Let me explain you several ways to implement it in Java.

So we have an Array or a List to start with, then we need to perform conversion.

Here the preset of data to start with.


private final Integer[] BASE_ARRAY = new Integer[] {1, 10, 99};
private final List RESULT_LIST = new ArrayList() {{
    add(1);
    add(10);
    add(99);
}};

private final List BASE_LIST = new ArrayList() {{
    add(14);
    add(42);
    add(2018);
}};
private final Integer[] RESULT_ARRAY = new Integer[] {14, 42, 2018};

Convert Array to List

How many ways can you think of to convert Array to List in Java?

It should be more than one, I think.

Okay, so I have come up with four ways to implement it.

1. Using loop or interation

Very basic, right?

List list1 = new ArrayList<>();
for(Integer i: BASE_ARRAY) {
    list1.add(i);
}

Assert.assertThat(list1, is(RESULT_LIST));

This way always works but not that cool. Try next method.

2. Using Collections.addAll() method

This is a pre-created spaghetti method for above method.

List list2 = new ArrayList<>();
Collections.addAll(list2, BASE_ARRAY);

Assert.assertThat(list2, is(RESULT_LIST));

This Collections.addAll() does not differ much from using loop because it uses the loop itself to add element to the list. Here’s the source.

public static  boolean addAll(Collection<? super T> c, T... elements) {
    boolean result = false;
    for (T element : elements)
        result |= c.add(element);
    return result;
}

3. Using Arrays.asList() method

You might know this already since it is a pretty common method used for such a conversion nowadays.

List list3 = Arrays.asList(BASE_ARRAY);

Assert.assertThat(list3, is(RESULT_LIST));

You can refer to Java documentation about Arrays.asList().

4. Using Stream API in Java 8

Stream API in Java 8 is pretty much powerful that help developers to process collections on the fly and transform data into any condition we want, just like functional programming.

List list4 = Arrays.stream(BASE_ARRAY).collect(Collectors.toList());

Assert.assertThat(list4, is(RESULT_LIST));

Convert List to Array

Similarly to convert from Array to List, we will need to convert from List to Array pretty much often.

Here’s the take.

1. Using loop or iteration

No doubt that this is the very first option that any Java developers will think of.

Integer[] array1 = new Integer[BASE_LIST.size()];
for(int i = 0; i < BASE_LIST.size(); i++) {
    array1[i] = BASE_LIST.get(i);
}

Assert.assertArrayEquals(RESULT_ARRAY, array1);

2. Using List.toArray() method

A convenient method for Java development so we don’t have to re-invent anything.

Integer[] array2 = BASE_LIST.toArray(new Integer[BASE_LIST.size()]);

Assert.assertArrayEquals(RESULT_ARRAY, array2);

3. Even better of List.toArray()

Maybe you do not know the unknown part of the toArray() method.

Integer[] array3 = BASE_LIST.toArray(new Integer[0]);

Assert.assertArrayEquals(RESULT_ARRAY, array3);

Want to know why this version is much better? Check out this article – Arrays of Wisdom of the Ancients.

Source code

You can find the code here: https://gist.github.com/petehouston/56b28ebc13b34c29a0c21a35b04247c8