Converting array to list in Java

ID : 1151

viewed : 125

Tags : javaarrayslistdata-conversionjava

Top 5 Answer for Converting array to list in Java

vote vote

94

In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.

You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.

Integer[] spam = new Integer[] { 1, 2, 3 }; List<Integer> list = Arrays.asList(spam); 

See this code run live at IdeOne.com.

vote vote

81

In Java 8, you can use streams:

int[] spam = new int[] { 1, 2, 3 }; Arrays.stream(spam)       .boxed()       .collect(Collectors.toList()); 
vote vote

70

Speaking about conversion way, it depends on why do you need your List. If you need it just to read data. OK, here you go:

Integer[] values = { 1, 3, 7 }; List<Integer> list = Arrays.asList(values); 

But then if you do something like this:

list.add(1); 

you get java.lang.UnsupportedOperationException. So for some cases you even need this:

Integer[] values = { 1, 3, 7 }; List<Integer> list = new ArrayList<Integer>(Arrays.asList(values)); 

First approach actually does not convert array but 'represents' it like a List. But array is under the hood with all its properties like fixed number of elements. Please note you need to specify type when constructing ArrayList.

vote vote

67

It seems little late but here are my two cents. We cannot have List<int> as int is a primitive type so we can only have List<Integer>.

Java 8 (int array)

int[] ints = new int[] {1,2,3,4,5}; List<Integer> list11 =Arrays.stream(ints).boxed().collect(Collectors.toList());  

Java 8 and below (Integer array)

Integer[] integers = new Integer[] {1,2,3,4,5}; List<Integer> list21 =  Arrays.asList(integers); // returns a fixed-size list backed by the specified array. List<Integer> list22 = new ArrayList<>(Arrays.asList(integers)); // good List<Integer> list23 = Arrays.stream(integers).collect(Collectors.toList()); //Java 8 only 

Need ArrayList and not List?

In case we want a specific implementation of List e.g. ArrayList then we can use toCollection as:

ArrayList<Integer> list24 = Arrays.stream(integers)                           .collect(Collectors.toCollection(ArrayList::new)); 

Why list21 cannot be structurally modified?

When we use Arrays.asList the size of the returned list is fixed because the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. So if we add or remove elements from the returned list, an UnsupportedOperationException will be thrown. So we should go with list22 when we want to modify the list. If we have Java8 then we can also go with list23.

To be clear list21 can be modified in sense that we can call list21.set(index,element) but this list may not be structurally modified i.e. cannot add or remove elements from the list. You can also check this answer of mine for more explanation.


If we want an immutable list then we can wrap it as:

List<Integer> list22 = Collections.unmodifiableList(Arrays.asList(integers)); 

Another point to note is that the method Collections.unmodifiableList returns an unmodifiable view of the specified list. An unmodifiable view collection is a collection that is unmodifiable and is also a view onto a backing collection. Note that changes to the backing collection might still be possible, and if they occur, they are visible through the unmodifiable view.

We can have a truly immutable list in Java 9 and 10.

Truly Immutable list

Java 9:

String[] objects = {"Apple", "Ball", "Cat"}; List<String> objectList = List.of(objects); 

Java 10 (Truly Immutable list):

We can use List.of introduced in Java 9. Also other ways:

  1. List.copyOf(Arrays.asList(integers))
  2. Arrays.stream(integers).collect(Collectors.toUnmodifiableList());
vote vote

52

The problem is that varargs got introduced in Java5 and unfortunately, Arrays.asList() got overloaded with a vararg version too. So Arrays.asList(spam) is understood by the Java5 compiler as a vararg parameter of int arrays.

This problem is explained in more details in Effective Java 2nd Ed., Chapter 7, Item 42.

Top 3 video Explaining Converting array to list in Java

Related QUESTION?