How to Convert Array to List in Java

  • Post last modified:March 24, 2023
  • Reading time:3 mins read

Discussing approaches to convert array to mutable and immutable list

Introduction

  • In this article, we will see various approaches to convert fixed-size arrays in java to List.
  • We will also cover how to convert arrays to mutable and immutable lists
  • Consider primitive array as shown below:

Input 

  • Our input array contains an array of primitive int types.
int[] x = { 1,2,3,4 };

Converting to Mutable List

  • Now let’s consider that we need a mutable list from the array. We can use java streams API to convert primitive array to object type and then using collector we can convert it to list.
  • Collectors.toList returns a mutable list but as per the doc it’s not guaranteed.
var list = Arrays.stream(x)
                .boxed()
                .collect(Collectors.toList());
  • If we really need a mutability guarantee, then we can modify the above code a little bit.
  • toCollection method can accept type arguments and now we can pass ArrayList type for List. This returns a mutable list.
var list = Arrays.stream(x)
                .boxed()
                .collect(Collectors.toCollection(ArrayList::new));

Converting to Immutable List

  • Collections class provides unmodifiableList method, which converts modifiable to list unmodifiable.
  • Since this method expects a list as input, we need to convert our input array to modifiableList first and then use unmodifiableList method.
List<Integer> modifiableList = 
                Arrays.stream(x).boxed().collect(Collectors.toList());

var unmodifiableList = Collections.unmodifiableList(modifiableList);

unmodifiableList.add(1);
  • Or better approach is to convert the input array to stream and then box it Object type, then use toList() which returns the immutable list.
// immutable shorten
         Arrays.stream(x)
                .boxed()
                .toList();
  • Java9 provides List.of() which we can use to get the immutable list. This method accepts elements that consist in the list or varargs of the element.
  • So we can pass boxed type array as varargs to separate method and use it for List.of argument, then it can return the immutable list as output.
public static void main(String[] args) {
    int[] x = { 1,2,3,4 };
    Integer[] integers = Arrays.stream(x).boxed().toArray(Integer[]::new);

    List<Integer> immutableList = toList(integers);
    //immutableList.add(1); error
}


private static List<Integer> toList(Integer... nums){
    return List.of(nums); // using java 9 List.of()
}

Bonus

  • Array.asList method returns List as output. Although this list is mutable, this is in a fixed size. So if we try to modify it by adding more elements it will throw an error.
  • Hence one of the solutions to overcome that is to pass the result of Array.asList to a new ArrayList constructor, which will return mutable and dynamic size ArrayList.
// fixed size mutable list
   var m = Arrays.asList(Arrays.stream(x).boxed().toArray(Integer[]::new));
      //  m.add(3); // doesnt work

         // mutable non-fixed size mutable list
   var l = new ArrayList<>(Arrays.asList(Arrays.stream(x).boxed().toArray(Integer[]::new)));
   l.add(1); //works

Constructor

  • In this article, we explore different approaches to convert an array to a list in java. 
  • We also discuss how we can get mutable and immutable lists from primitive arrays.

Before You Leave

  • Let me know if I can be of any help to your career, I would love to chat or jump on a call. you can connect me over Linkedin.
  • If you like this content consider supporting.
  • If you want to upskill your Java skills, you should definitely check out
    Java Programming Masterclass updated to Java 17
    [ 750,000 students already enrolled, with 4.5 stars]

Leave a Reply