DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to initialize ArrayList in Java

In the last post, I discussed Generic-array and important methods. Here we share some ways to initialize an ArrayList with examples.

The Java ArrayList may be initiated in a number of ways depending on the needs. In this tutorial, we will learn to initialize an ArrayList based on multiple use-cases that are often seen.

title: "How to initialize ArrayList in Java"
tags: java

canonical_url: https://kodlogs.com/blog/2142/how-to-initialize-arraylist-in-java

  1. Initialization ArrayList in one line

1.1. Arrays.asList () - Initialize ArrayList of various

To initialize an ArrayList in a statement line, get all the elements in an array using the method Arrays.asList and pass an array of arguments to the constructor ArrayList.

Create an ArrayList in a single statement

ArrayList names = new ArrayList (Arrays.asList ( "alex", "brian", "charles"));

System.out.println (name);

Output Program.

entertain

[Alex, brian, charles]

1.2. List.of () - Changed the list - Java 9

We can use List.of () static factory method to create a list of changes. The only drawback is that the add operation is not supported in this list.

Create a list in a single statement

List names = List.of ( "alex", "brian");

System.out.println (name);

Output Program.

entertain

[Alex, brian]

  1. Create an ArrayList and add objects - ArrayList constructor

Using the ArrayList constructor is the traditional approach. We create an empty ArrayList using the constructor and add an element to the list using the () method add. We can add an element either individually, or we can pass another collection to add all the elements in one step.

Create an ArrayList with a constructor

ArrayList names = new ArrayList <> ();

// 1. Adding the elements one by one

names.add ( "Alex");

names.add ( "brian");

names.add ( "Charles");

System.out.println (name);

HashMap Details = new HashMap <> ();

details.put ( "keanu", 23);

details.put ( "max", 24);

details.put ( "john", 53);

// 2. Add the elements of other collections

names.addAll (details.keySet ());

System.out.println (name);

Output Program.

entertain

[Alex, brian, charles]

[Alex, brian, charles, max, john, keanu]

  1. Initialization ArrayList list

At times, we may need to initialize the ArrayList from the list.

ArrayList list

List > sign = new ArrayList <> ();

marks.add (Arrays.asList (10, 20, 30));

marks.add (Arrays.asList (40, 50, 60));

marks.add (Arrays.asList (70, 80, 90));

for (List mark: mark) {

System.out.println (mark);

}

Output Program.

entertain

[10, 20, 30]

[40, 50, 60]

[70, 80, 90]

Please note that Arrays.asList () does not return java.util.ArrayList example. It returns java.util.Arrays $ ArrayList instance instead.

So, if you should have any ArrayList, then make for Arrays.asList ArrayList () for instance underway.

marks.add (new ArrayList (Arrays.asList (10, 20, 30)));

It's all about creating an ArrayList in Java. Drop me your questions in the comments.

Have a good study !!

Top comments (0)