BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Add Multiple Items to an ArrayList in Java

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to add multiple items to an ArrayList in Java.

1. Add multiple items using adAll() method

The addAll() can be used to add multiple items to an ArrayList. This method takes another list as an argument and add the elements of the passed list to the ArrayList.

Here, we have a list that contains multiple items, we are adding these multiple items to the newly created ArrayList using addAll() method as shown below.

import java.util.*;
public class JavaExample
{
  public static void main(String[] args)
  {
    List<String> list = Arrays.asList("hi", "hello", "bye");
    ArrayList<String> al = new ArrayList<String>();
    al.addAll(list);
    System.out.println("Original List elements:"+list);
    System.out.println("ArrayList elements:"+al);

    //Adding element to the arraylist
    al.add("hey");
    System.out.println("Original List elements:"+list);
    System.out.println("ArrayList elements:"+al);
  }
}

Output:

Original List elements:[hi, hello, bye]
ArrayList elements:[hi, hello, bye]
Original List elements:[hi, hello, bye]
ArrayList elements:[hi, hello, bye, hey]

2. Add multiple items using Collections.adAll() method

The another way of adding multiple items to an ArrayList is using the Collections.addAll() method. This method takes two argument. The first argument is the collection where the elements needs to be added and the second argument is the collection from where the items are copied.
For example: The following statement will add the specified elements to the list.

Collections.addAll(list, "Item1", "Item2",);

Let’s take a look at the complete example:
Here, we have created an ArrayList list. We are adding multiple elements to it using Collections.addAll() method by passing list as the first argument and items as the second argument.

import java.util.*;
public class JavaExample
{
  public static void main(String[] args)
  {
    ArrayList<String> list = new ArrayList<String>();
    Collections.addAll(list, "Mango","Apple", "Banana");
    System.out.println("ArrayList elements:"+list);
  }
}

Output:

ArrayList elements:[Mango, Apple, Banana]

Recommended Articles:

  • How to get the last element of an ArrayList
  • Remove duplicates from an ArrayList in Java
  • Difference between Array and ArrayList
❮ Java Collections

Top Related Articles:

  1. Convert ArrayList to Array in Java
  2. How to Convert an array to ArrayList in java
  3. How to loop HashMap in java
  4. How to get the last element of Arraylist?
  5. Multilevel inheritance in java with example

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap