Skip to main content

Different ways to initialize ArrayList in Java

How to initialize ArrayList in Java

In this article, we are going to learn different ways to initialize ArrayList in Java. If you don't know about Arraylist, read this article to know All about the ArrayList in Java.

Content of the Article


How to initialize ArrayList in Java - using Default Constructor?

We can initialize ArrayList in Java using default constructor and then we can add elements into the ArrayList with the help of add() method. For Example
Syntax
ArrayList<T> arrayList = new ArrayList<>();

Java init ArrayList Example 1: Initialize ArrayList in Java - using Default Constructor

import java.util.ArrayList;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languageList = new ArrayList<>();
                languageList.add("Java");
                languageList.add("C++");
                languageList.add("Python");
                languageList.add("NodeJS");
                languageList.add("JavaScript");
                System.out.println("Language ArrayList : " + languageList);
        }
}
Output
Language ArrayList : [Java, C++, Python, NodeJS, JavaScript]
This is a very simple method to initialize ArrayList in Java, but it is not a convenient way as it can be very hard to add hundreds or thousands of elements. We are going to learn different ways to initialize ArrayList in this topic that can help in these types of scenarios.

How to initialize ArrayList in Java - using Copy Constructor?

We can use copy constructor of ArrayList to copy elements from one ArrayList to another ArrayList.
Syntax
ArrayList<T> copyArrayList = new ArrayList<>(originalArrayList);

Java init ArrayList Example 2: Initialize ArrayList in Java - using Copy Constructor

import java.util.ArrayList;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languageList = new ArrayList<>();
                languageList.add("Java");
                languageList.add("C++");
                languageList.add("Python");
                languageList.add("NodeJS");
                languageList.add("JavaScript");
                ArrayList<String> copyLanguageList = new ArrayList<>(languageList);
                System.out.println("Language ArrayList : " + copyLanguageList);
        }
}
Output
Language ArrayList : [Java, C++, Python, NodeJS, JavaScript]

How to initialize ArrayList in Java - using Arrays.asList() method?

In the java.util.Arrays class there is a static method asList() which can be used to initialize ArrayList in Java.
Syntax
List<T> list = Arrays.asList(element1, element2, ......., elementN);
ArrayList<T> arrayList = new ArrayList<>(list);
This method return reference of List interface and we can use Copy Constructor to initialize ArrayList in Java.

Java init ArrayList Example 3: Initialize ArrayList in Java - using Arrays.asList() method

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

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languageList = new ArrayList<>(
                        Arrays.asList("Java", "C++", "Python", "NodeJS", "JavaScript"));
                System.out.println("Language ArrayList : " + languageList);
        }
}
Output
Language ArrayList : [Java, C++, Python, NodeJS, JavaScript]

How to initialize ArrayList in Java - using stream API?

With the help of stream API, introduced in Java 8, we can initialize ArrayList. We can start a stream using of() static method of java.util.stream.Stream class. And then, by using collect() method and java.util.stream.Collectors, we can collect the stream into the ArrayList.
Syntax
List<T> list = Stream.of(element1, element2, ......., elementN).collect(Collectors.toList());
ArrayList<T> arrayList = new ArrayList<>(list);

Java init ArrayList Example 4: Initialize ArrayList in Java - using stream API

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languageList = new ArrayList<>(
                        Stream.of("Java", "C++", "Python", "NodeJS", "JavaScript")
                              .collect(Collectors.toList()));
                System.out.println("Language ArrayList : " + languageList);
        }
}
Output
Language ArrayList : [Java, C++, Python, NodeJS, JavaScript]

Related Posts:


Conslusion

In this article, we have learned different ways to initialize ArrayList in Java. We have learned how to initialize ArrayList using Arrays.asList() method, stream APIs, copy constructor etc.

Comments

Popular posts from this blog

How HashMap works internally in java

How Hashmap works Internally in Java In this article, we are going to see how HashMap internally works in java. Also, we will have a look at what Java 8 made changes on the internal working of Hashmap to make it faster. What is Hashmap and Hashing in java? How Hashmap works Internally in Java is majorly dependent upon the Hashing Principle. So, Before going to learn how HashMap works internally in java, lets first understand what is HashMap and hashing. HashMap : A HashMap is a map used to store mappings of key-value pairs. Also, it works on the Principle of Hashing. To know more about the HashMap, visit this article: HashMap in Java Hashing Principle : Simply, Hashing is a method used to produce an integer value from an object and this integer value known as the hash value. In HashMap, the key object is used for Hashing.
Internal Struture of the HashMap in java For internal working of HashMap, HashMap maintains an array of bucket, each bucket is a linked-list and linked list is a li…

HashMap in Java

HashMap in Java In this article, we are going to learn about the most important class of Java known as HashMap. HashMap in java is the most used map interface and it is used in our regular or day-to-day programming practices.
What is HashMap in java? Lets first start with basic definition of HashMap,
HashMap is a map which store key-value pair, where each key is mapped with a value.
HashMap defined in java.util package and implements map interface which provides functionalities to perform basic operation like store, retrieve, size of the map etc. It is a part of Java Collection Framework. Time complexity of HashMap in Java? : Time complexity to store and retrieve data from the HashMap is O(1) in the Best Case. But it can be O(n) in the worst case and after the changes made in Java 8 the worst case time complexity can be O(log n) atmost. Internal working of HashMap in java HashMap maintains an array of the buckets, where each bucket is a linked-list and the linked list is a list of…

ArrayList in Java

ArrayList in Java In this article, we are going to discuss ArrayList in Java. ArrayList is a class from the Java Collection Framework and widely in our programs. We use ArrayList as a substitute for the array. So, it is important to know ArrayList in detail. We are going to see what is ArrayList in Java, how we can create the instance of ArrayList and the methods provided by the ArrayList in Java.

If you already familiar to ArrayList, you may learn common programs related to ArrayList.
Table of ContentsWhat is ArrayList in Java?Hierarchy of the ArrayList in java?How to create objects of ArrayList in Java?How to create an ArrayList of Custom Objects?How to iterate ArrayList in Java?How to get the elements by index from ArrayList in Java?How to check whether an elements is stored in the ArrayList in Java?How to remove elements from the ArrayList in Java?Few more methods of ArrayList in Java?
What is ArrayList in Java? ArrayList in Java is a List in which we can store and retrieve elemen…