Sai A Sai A
Updated date Jun 29, 2023
Learn how to convert a List to an Array in Java with this beginner's guide. Explore different methods, such as using the toArray() method, leveraging the Stream API, utilizing Apache Commons Lang library.

Introduction:

Java provides a rich set of data structures to handle various programming needs. Two commonly used data structures are lists and arrays. While lists offer flexibility in size and dynamic operations, arrays provide a fixed-size structure and efficient random access. There are situations where we might need to convert a list into an array or vice versa to leverage the advantages of each data structure. In this blog, we will explore different methods to convert a List to an Array in Java, along with code examples and explanations.

Method 1: Using the toArray() Method

The simplest way to convert a List to an Array in Java is by utilizing the toArray() method provided by the List interface. The toArray() method converts the List into an array of the desired type. Here's an example:

import java.util.*;

public class ListToArrayExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String[] array = list.toArray(new String[0]);

        for (String element : array) {
            System.out.println(element);
        }
    }
}

Output:

Apple
Banana
Orange

In the above example, we start by creating a List of Strings called "list" and add three fruits to it. To convert the list to an array, we use the toArray() method, passing an empty String array as an argument. The method converts the list into an array of the same type, which is assigned to the "array" variable. Finally, we iterate over the array and print each element.

Method 2: Using the Stream API

Java 8 introduced the Stream API, which provides a powerful and concise way to manipulate collections. We can leverage this API to convert a List to an Array as well. Here's an example:

import java.util.*;
import java.util.stream.*;

public class ListToArrayExample {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        Integer[] array = list.stream().toArray(Integer[]::new);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1, 2, 3, 4, 5]

In this example, we start with a List of integers called "list" by using the Arrays.asList() method. To convert the list to an array, we create a stream from the list using the stream() method. Then, we use the toArray() method provided by the Stream API, passing the constructor reference of the desired array type as an argument. The toArray() method converts the stream elements into an array, which is assigned to the "array" variable. Finally, we print the array using Arrays.toString().

Method 3: Using Apache Commons Lang

If you have Apache Commons Lang library available in your project, you can utilize the ArrayUtils class to convert a List to an Array. Here's an example:

import org.apache.commons.lang3.ArrayUtils;

import java.util.*;

public class ListToArrayExample {
    public static void main(String[] args) {
        List<Double> list = Arrays.asList(1.2, 3.4, 5.6);

        Double[] array = ArrayUtils.toArray(list);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1.2, 3.4, 5.6]

In this example, we start with a List of doubles called "list" by using the Arrays.asList() method. We convert the list to an array using the ArrayUtils.toArray() method from the Apache Commons Lang library. The toArray() method converts the list into an array of the desired type, which is assigned to the "array" variable. Finally, we print the array using Arrays.toString().

Conclusion:

This blog explored various methods to convert a List to an Array in Java. We started with the simple toArray() method, followed by utilizing the Stream API and Apache Commons Lang library. Understanding these conversion techniques will help you handle different scenarios efficiently when working with lists and arrays in your Java programs.

Comments (0)

There are no comments. Be the first to comment!!!