Create mutable List from array?

JavaArraysListMutable

Java Problem Overview


I have an array I'd like to turn into a List, in order to modify the contents of the array.

Stack Overflow has plenty of questions/answers that address Arrays.asList() and how it only provides a List view of the underlying array, and how attempting to manipulate the resulting List will generally throw an UnsupportedOperationException as methods used to manipulate the list (e.g. add(), remove(), etc.) are not implemented by the List implementation provided by Arrays.asList().

But I can't find an example of how to turn an array into a mutable List. I suppose I can loop through the array and put() each value into a new List, but I'm wondering if there's an interface that exists to do this for me.

Java Solutions


Solution 1 - Java

One simple way:

Foo[] array = ...;
List<Foo> list = new ArrayList<Foo>(Arrays.asList(array));

That will create a mutable list - but it will be a copy of the original array. Changing the list will not change the array. You can copy it back later, of course, using toArray.

If you want to create a mutable view onto an array, I believe you'll have to implement that yourself.

Solution 2 - Java

And if you are using google collection API's (Guava):

Lists.newArrayList(myArray);

Solution 3 - Java

This simple code using the Stream API included in Java 8 creates a mutable list (or view) containing the elements of your array:

Foo[] array = ...;
List<Foo> list = Stream.of(array).collect(Collectors.toCollection(ArrayList::new));

Or, equally valid:

List<Foo> list = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new));

Solution 4 - Java

If you're using Eclipse Collections (formerly GS Collections), you can use FastList.newListWith(...) or FastList.wrapCopy(...).

Both methods take varargs, so you can create the array inline or pass in an existing array.

MutableList<Integer> list1 = FastList.newListWith(1, 2, 3, 4);

Integer[] array2 = {1, 2, 3, 4};
MutableList<Integer> list2 = FastList.newListWith(array2);

The difference between the two methods is whether or not the array gets copied. newListWith() doesn't copy the array and thus takes constant time. You should avoid using it if you know the array could be mutated elsewhere.

Integer[] array2 = {1, 2, 3, 4};
MutableList<Integer> list2 = FastList.newListWith(array2);
array2[1] = 5;
Assert.assertEquals(FastList.newListWith(1, 5, 3, 4), list2);

Integer[] array3 = {1, 2, 3, 4};
MutableList<Integer> list3 = FastList.wrapCopy(array3);
array3[1] = 5;
Assert.assertEquals(FastList.newListWith(1, 2, 3, 4), list3);

Note: I am a committer for Eclipse Collections.

Solution 5 - Java

myNewArrayList = new ArrayList<>(Arrays.asList(myArray));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionericsocoView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavavsinghView Answer on Stackoverflow
Solution 3 - JavaMikaelFView Answer on Stackoverflow
Solution 4 - JavaCraig P. MotlinView Answer on Stackoverflow
Solution 5 - JavaSidView Answer on Stackoverflow