Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ArrayList in Java
Next article icon

Initialize an ArrayList in Java

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.

  • ArrayList inherits the AbstractList class and implements the List interface.
  • ArrayList is initialized by a size, however, the size can increase if the collection grows or shrink if objects are removed from the collection.
  • Java ArrayList allows us to randomly access the list.
  • ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases (see this for details).
  • ArrayList in Java can be seen as similar to a vector in C++.

The image below demonstrates the Java Collection Framework hierarchy.

java-arraylist

Methods

Below are the various methods to initialize an ArrayList in Java

1. Initialization with add()

Syntax:

ArrayList<Type> al= new ArrayList<Type>();

al.add(“Geeks”);

al.add(“for”);

al.add(“Geeks”);

Example 1: This example demonstrates how to create an ArrayList using the add() method.

Java
// Java code to illustrate initialization // of ArrayList using add() method  import java.util.*;  public class Geeks {     public static void main(String args[])     {          // create a ArrayList String type         ArrayList<String> al = new ArrayList<String>();          // Initialize an ArrayList with add()         al.add("Geeks");         al.add("for");         al.add("Geeks");          // print ArrayList         System.out.println("ArrayList : " + al);     } } 

Output
ArrayList : [Geeks, for, Geeks] 

Note: Now, we are going to discuss the shorthand version of this method.


Example 2: This example demonstrates a shortcut way to create and fill an ArrayList with values in one step.

Java
// Java code to illustrate initialization // of ArrayList using add() method  import java.util.*;  public class Geeks {     public static void main(String args[])     {          // create a ArrayList String type         // and Initialize an ArrayList with add()         ArrayList<String> al = new ArrayList<String>() {             {                 add("Geeks");                 add("for");                 add("Geeks");             }         };          // print ArrayList         System.out.println("ArrayList : " + al);     } } 

Output
ArrayList : [Geeks, for, Geeks] 


2. Initialization using asList()

Syntax:

ArrayList<Type> obj = new ArrayList<Type>(

Arrays.asList(Obj A, Obj B, Obj C, ….so on));

Example: This example demonstrates how to create and initalize an ArrayList using the asList() method.

Java
// Java code to illustrate initialization // of ArrayList using asList method  import java.util.*;  public class Geeks {     public static void main(String args[])     {          // create a ArrayList String type         // and Initialize an ArrayList with asList()         ArrayList<String> al = new ArrayList<String>(             Arrays.asList("Geeks",                           "for",                           "Geeks"));          // print ArrayList         System.out.println("ArrayList : " + al);     } } 

Output
ArrayList : [Geeks, for, Geeks]


3. Initialization using List.of() Method

Syntax:

List<Type> obj = new ArrayList<>(

List.of(Obj A, Obj B, Obj C, ….so on));

Example: This example demonstrates how to create and initalize an ArrayList using the List.of() method.

Java
// Java code to illustrate initialization // of ArrayList using List.of() method  import java.util.*;  public class Geeks {     public static void main(String args[])     {          // create a ArrayList String type         // and Initialize an ArrayList with List.of()         List<String> al = new ArrayList<>(             List.of("Geeks",                     "for",                     "Geeks"));          // print ArrayList         System.out.println("ArrayList : " + al);     } } 

Output
ArrayList : [Geeks, for, Geeks]


4. Initialization using another Collection

Syntax:

List gfg = new ArrayList(collection);

Example: This example demonstrates how to create a new ArrayList by copying elements from an existing collection.

Java
// Java code to illustrate initialization // of ArrayList using another collection  import java.util.*;  public class Geeks {     public static void main(String args[])     {          // create another collection         List<Integer> l = new ArrayList<>();         l.add(1);         l.add(2);         l.add(3);         l.add(4);         l.add(5);          // create a ArrayList Integer type         // and Initialize an ArrayList with arr         List<Integer> l1 = new ArrayList<Integer>(l);          // print ArrayList         System.out.println("ArrayList : " + l1);     } } 

Output
ArrayList : [1, 2, 3, 4, 5]


5. Initialization using stream() and collect() Methods

Syntax:

ArrayList<Type> listName = Stream.of(element1, element2, …, elementN).collect(Collectors.toCollection(ArrayList::new));

Example: This example demonstrates how to create an ArrayList from a Stream using Java 8’s Stream API and Collectors.

Java
// creating arraylist using java8 stream API  import java.util.ArrayList; import java.util.stream.Collectors; import java.util.stream.Stream;  public class Geeks {     public static void main(String args[])     {          // create a stream of elements using Stream.of()         // method collect the stream elements into an         // ArrayList using the collect() method and         // Collectors.toCollection() method         ArrayList<String> al             = Stream.of("Geeks", "For", "Geeks")                   .collect(Collectors.toCollection(                       ArrayList::new));                                // print the ArrayList         System.out.println(al);      } } 

Output
[Geeks, For, Geeks]


Next Article
ArrayList in Java

R

Rajput-Ji
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Java-ArrayList
  • Java-List-Programs
  • Technical Scripter 2018
Practice Tags :
  • Java

Similar Reads

  • How to Initialize an Array in Java?
    An array in Java is a linear data structure, which is used to store multiple values of the same data type. In array each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java.
    6 min read
  • Initialize HashMap in Java
    HashMap in Java is a part of the java.util package and allows storing key-value pairs. Initializing a HashMap can be done in multiple ways, including static blocks, utility methods from the Collections class, and modern approaches provided by Java 8 and Java 9. This article will guide you through th
    4 min read
  • ArrayList in Java
    Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac
    10 min read
  • Iterating over ArrayLists in Java
    ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With
    5 min read
  • Internal Working of ArrayList in Java
    ArrayList is a resizable array implementation in Java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object classes. ArrayList class in Java has 3 constructors. It has its own version of readObject and wri
    10 min read
  • Array vs ArrayList in Java
    In Java, an Array is a fixed-sized, homogenous data structure that stores elements of the same type whereas, ArrayList is a dynamic-size, part of the Java Collections Framework and is used for storing objects with built-in methods for manipulation. The main difference between array and ArrayList is:
    5 min read
  • Array of ArrayList in Java
    We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce the space. One such solution is to use jagged array when we know the length of each row in the array, but the problem arises when we do not specifically know
    2 min read
  • Array to ArrayList Conversion in Java
    In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it. Methods to Convert Array to an ArrayList1. Using add() Method to Manually add t
    4 min read
  • Double Brace Initialization in Java
    The combination of two separate processes in Java is known as Double Brace Initialization in Java. As the name suggests, there are two braces {{ included in it. A single brace { is nothing new for programmers. The first brace in the double brace initialization is used to create an anonymous inner cl
    4 min read
  • Find first and last element of ArrayList in java
    Prerequisite: ArrayList in Java Given an ArrayList, the task is to get the first and last element of the ArrayList in Java, Examples: Input: ArrayList = [1, 2, 3, 4] Output: First = 1, Last = 4 Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: First = 12, Last = 89 Approach: Get the ArrayList
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences