May 26, 2022

Convert Array to ArrayList in Java

In the post How to Convert ArrayList to Array in Java we have already seen different ways of doing that conversion. This post is about the other way round how to convert array to ArrayList in Java.

Options for converting array to ArrayList in Java

  1. Using Arrays.asList() method. See example.
  2. Using Collections.addAll() method. See example.
  3. Java 8 onward you can use Java Stream API for converting array to ArrayList in Java. See example.
  4. Doing it manually without using any API methods. See example.

Using Arrays.asList() method

Using Arrays.asList() method you can convert array to ArrayList. The returned list by this method is backed by the array, thus any change in the List will be reflected in array too.

import java.util.Arrays;
import java.util.List;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    // Converting array to arraylist
    List<String> carList = Arrays.asList(carArray);
    System.out.println("List Elements- " + carList);
    // changing element in List
    carList.set(1, "Mercedes");
    System.out.println("Array Elements- " + Arrays.toString(carArray));
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]
Array Elements- [Audi, Mercedes, BMW, Mini Cooper]

As you can see from the output when an element at index 1 is changed in the ArrayList that change is reflected to the array too.

Another drawback of this method is that returned list is fixed you can’t add or remove element to the returned List. Any attempt to add or remove elements will throw java.lang.UnsupportedOperationException.

import java.util.Arrays;
import java.util.List;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    // Converting array to arraylist
    List<String> carList = Arrays.asList(carArray);
    System.out.println("List Elements- " + carList);
    // trying to remove element
    carList.remove("Audi");
    System.out.println("Array Elements- " + Arrays.toString(carArray));
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]
Exception in thread "main" java.lang.UnsupportedOperationException: remove
	at java.base/java.util.Iterator.remove(Iterator.java:102)
	at java.base/java.util.AbstractCollection.remove(AbstractCollection.java:299)
	at com.knpcode.ArrayToList.main(ArrayToList.java:14)

If you need to modify the list you get by converting array to ArrayList then you should create a new ArrayList using the returned list.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    // Converting array to arraylist
    List<String> carList = new ArrayList<String>(Arrays.asList(carArray));
    System.out.println("List Elements- " + carList);
    // changing element in List
    carList.remove("Audi");
    System.out.println("Array Elements- " + Arrays.toString(carArray));
    System.out.println("List Elements- " + carList);
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]
Array Elements- [Audi, Jaguar, BMW, Mini Cooper]
List Elements- [Jaguar, BMW, Mini Cooper]

As you can see from the output by creating a new ArrayList you can structurally modify it and now changes are not reflected to the array too.

Using Collections.addAll() method

You can also use Collections.addAll() method to convert array to ArrayList in Java. You need to pass both List and array as arguments to this method.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    List<String> carList = new ArrayList<String>();
    Collections.addAll(carList, carArray);
    System.out.println("List Elements- " + carList);
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]

Java Stream API’s collect() method

Java 8 onward you can also use Collectors in Java Stream API for converting array to ArrayList in Java.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    List<String> carList = Stream.of(carArray).collect(Collectors.toList());
    System.out.println("List Elements- " + carList);
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]

Without using any API method

You can convert array to ArrayList by writing your own logic too rather than using any API method. It required just a simple for loop in which you need to add iterated element of array to ArrayList.

import java.util.ArrayList;
import java.util.List;

public class ArrayToList {
  public static void main(String[] args) {
    String[] carArray = {"Audi", "Jaguar", "BMW", "Mini Cooper"};
    List<String> carList = new ArrayList<String>();
    for(String car : carArray) {
      carList.add(car);
    }
    System.out.println("List Elements- " + carList);
  }
}
Output
List Elements- [Audi, Jaguar, BMW, Mini Cooper]

That's all for the topic Convert Array to ArrayList in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment