Create mutable List from array? Create mutable List from array? arrays arrays

Create mutable List from array?


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.


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

Lists.newArrayList(myArray);


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));