How to Convert String Array to List in Java

In this Java tutorial, we will learn how to convert a String array into a List using both traditional Java and Java 8.

For example:

String Array : {"Java", "Python", "C++", "JavaScript"}

List        : [Java, Python, C++, JavaScript]

1. Traditional Java Program

The Arrays.asList() method can be used to convert a String array into a List.

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

public class StringArrayToList {
    public static void main(String[] args) {
        String[] array = {"Java", "Python", "C++", "JavaScript"};
        List<String> list = Arrays.asList(array);
        System.out.println("String Array: "
                + Arrays.toString(array));
        System.out.println("List: " + list);
    }
}

Output

String Array: [Java, Python, C++, JavaScript]
List: [Java, Python, C++, JavaScript]

Explanation

The Arrays.asList() method converts the array into a List:

List<String> list = Arrays.asList(array);

Here, array is a String array and list is a List<String>.

Important Note

The List returned by Arrays.asList() is fixed-size. You can change an existing element, but you cannot add or remove elements.

For example:

list.set(0, "C");

is allowed, but:

list.add("HTML");

will throw UnsupportedOperationException.

If you need a modifiable List, use:

List<String> list = new ArrayList<>(Arrays.asList(array));

2. Java 8 Program

Java 8 introduced the Stream API, which provides another way to convert an array into a List.

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

public class StringArrayToListJava8 {
    public static void main(String[] args) {
        String[] array = {"Java", "Python", "C++", "JavaScript"};
        List<String> list = Arrays.stream(array)
                .collect(Collectors.toList());
        System.out.println("String Array: "
                + Arrays.toString(array));
        System.out.println("List: " + list);
    }
}

Output

String Array: [Java, Python, C++, JavaScript]
List: [Java, Python, C++, JavaScript]

Explanation of Java 8 Program

First, we convert the array into a Stream:

Arrays.stream(array)

Then, collect() collects the stream elements into a List:

.collect(Collectors.toList())

The complete statement is:

List<String> list = Arrays.stream(array)
        .collect(Collectors.toList());

This is a common Java 8 approach when you also want to filter, transform, or process the array elements while creating the List.


3. Java 8 Using Arrays.asList()

It is also important to understand that Arrays.asList() works perfectly in Java 8:

String[] array = {"Java", "Python", "C++", "JavaScript"};

List<String> list = Arrays.asList(array);

You do not need Streams just because you are using Java 8.

Difference Between the Approaches

FeatureTraditional JavaJava 8 Stream
MethodArrays.asList()Arrays.stream()
Stream APINoYes
Lambda requiredNoNo
Simple conversionExcellentGood
Filtering/processingLess convenientExcellent
Java VersionJava 5+Java 8+

Which Method Should You Use?

If your only requirement is to convert a String array to a List, the simplest approach is:

List<String> list = Arrays.asList(array);

If you need to filter or modify elements while converting, Java 8 Streams are useful:

List<String> list = Arrays.stream(array)
        .filter(s -> s.length() > 4)
        .collect(Collectors.toList());

For example, the above code creates a List containing only Strings with more than four characters.

Conclusion

Converting a String array to a List is simple in Java. Arrays.asList() is the easiest option for a direct conversion, while the Java 8 Stream API is useful when additional processing such as filtering or transformation is required.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *