How to Initialize an ArrayList in One Line in Java

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Initialize an ArrayList in One Line in Java

There are several ways to initialize an ArrayList in one line in Java. Here are two possible approaches:

Approach 1: Using the Arrays.asList() method

You can use the Arrays.asList() method to initialize an ArrayList in one line. This method takes a variable number of arguments and returns a fixed-size List backed by the specified array. Here’s an example:

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

public class Main {
    public static void main(String[] args) {
        // Initialize ArrayList in one line
        List fruits = new ArrayList(Arrays.asList("Apple", "Banana", "Orange"));

        // <a href="https://www.squash.io/how-to-print-arraylist-in-java/">Print the ArrayList</a>
        System.out.println(fruits);
    }
}

This will output:

[Apple, Banana, Orange]

In the above example, we create a new ArrayList and pass the result of Arrays.asList() as an argument to the ArrayList constructor. This initializes the ArrayList with the elements “Apple”, “Banana”, and “Orange” in one line.

Related Article: How to Convert List to Array in Java

Approach 2: Using Java 9’s List.of() method

If you are using Java 9 or later, you can also use the List.of() method to initialize an ArrayList in one line. This method is a convenient way to create an immutable List with a fixed number of elements. Here’s an example:

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

public class Main {
    public static void main(String[] args) {
        // Initialize ArrayList in one line
        List fruits = new ArrayList(List.of("Apple", "Banana", "Orange"));

        // Print the ArrayList
        System.out.println(fruits);
    }
}

This will output:

[Apple, Banana, Orange]

In the above example, we create a new ArrayList and pass the result of List.of() as an argument to the ArrayList constructor. This initializes the ArrayList with the elements “Apple”, “Banana”, and “Orange” in one line.

Alternative ideas and suggestions

– If you don’t want to use the ArrayList constructor, you can also initialize an ArrayList using the add() method. Here’s an example:

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

public class Main {
    public static void main(String[] args) {
        // Initialize ArrayList in one line
        List fruits = new ArrayList();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Print the ArrayList
        System.out.println(fruits);
    }
}

This will output:

[Apple, Banana, Orange]

– If you know the size of the ArrayList in advance, you can also specify the initial capacity when creating the ArrayList. This can help improve performance if you know the approximate number of elements the ArrayList will hold. Here’s an example:

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

public class Main {
    public static void main(String[] args) {
        // Initialize ArrayList with initial capacity
        List fruits = new ArrayList(3);
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Print the ArrayList
        System.out.println(fruits);
    }
}

This will output:

[Apple, Banana, Orange]

Best practices

– When initializing an ArrayList in one line, consider using the Arrays.asList() method if you need a mutable ArrayList or the List.of() method if you need an immutable ArrayList.
– If you are using an older version of Java that does not support List.of(), consider upgrading to a newer version to take advantage of the convenience it offers.
– If you know the size of the ArrayList in advance and performance is a concern, consider specifying the initial capacity when creating the ArrayList.

Related Article: Tutorial: Sorted Data Structure Storage in Java

Tutorial: Enumeration Types and Data Structures in Java

Enumeration types in Java provide a powerful way to define a set of named values. In this article, we will explore how enumeration types can be used to hold data... read more

How to Easily Print a Java Array

Printing a Java Array can be done simply using either a loop or the Arrays class. This article provides a concise guide on the best practices for printing a Java... read more

How to Sort a List of ArrayList in Java

Sorting a List ArrayList in Java can be done efficiently using various methods. This article provides a step-by-step guide on sorting a List ArrayList in Java. It covers... read more

How to Declare and Initialize a New Array in Java

Declaring and initializing arrays in Java is a fundamental skill for any programmer. This article provides a step-by-step guide to help you understand and implement this... read more

How to Print an ArrayList in Java

Printing an ArrayList in Java can be done in multiple ways. One approach is to use a for-each loop, which allows you to iterate through the elements of the ArrayList and... read more

Saving Tree Data Structure in Java File: A Discussion

This article provides an in-depth exploration of methods for storing tree data structures in Java files. It covers topics such as file handling, serialization, utilizing... read more