Convert Array to List

JavaJavaBeginner
Practice Now

Introduction

In Java, an array is a collection of elements of the same data type, whereas a list is a collection of elements of any data type. In some scenarios, we may need to convert an array to a list in Java. There are three ways to achieve this. In this lab, we will guide you through the steps required to convert an array to a list in Java.

Create a Java file

Create a new Java file in the ~/project directory named ArrayToList.java.

cd ~/project
touch ArrayToList.java

Add import statements and class header

Add the following import statements and class header to the ArrayToList.java file

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

public class ArrayToList {
    public static void main(String[] args) {
        // Code to convert array to list
    }
}

Convert array to list using add() method

In this example, we are using the add() method to add each element of the array to a list explicitly. This method is the most intuitive way to convert an array to a list. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>();
for (String course : courses) {
    courseList.add(course);
}

Convert array to list using Arrays.asList() method

In this example, we are using the asList() method from the Arrays class to convert an array to a list. This method is more efficient than the previous method because it doesn't require traversing the entire array. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>(Arrays.asList(courses));

Convert array to list using Collections.addAll() method

In this example, we are using the addAll() method from the Collections class to convert an array to a list. This method accepts two parameters, the list and the array, and converts the array to a list. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>();
Collections.addAll(courseList, courses);

Compile and run the code

Save the ArrayToList.java file and compile it using the following command:

javac ArrayToList.java

Then, run the code using the following command:

java ArrayToList

View the output

After running the code, you can view the output in the terminal. The output will be the same for all three methods:

Bangalore
Mumbai
Delhi
Noida

Summary

In this lab, we learned how to convert an array to a list in Java. We discussed three methods to achieve this. The first method is the most intuitive but requires traversing the entire array. The other two methods are more efficient and don't require traversing the entire array. We also provided a step-by-step guide to convert an array to a list using each of these methods.

Other Java Tutorials you may like