Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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

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]

R

rajput-ji
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2018
  • Java-ArrayList
  • Java-List-Programs
Practice Tags :
  • Java

Similar Reads

    How to Initialize an Array in Java?
    An array in Java is a linear data structure that is used to store multiple values of the same data type. In an 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
    5 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
    9 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:
    4 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