
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Add the data from the Specified Collection in the Current Collection
In Java, a collection is an object that allows us to group several numbers of objects as a single unit.
The Collection interface is the root of the collection framework. We can perform various operations on collection like adding, removing, iterating, searching and retrieving objects.
In this article, we will discuss Java programs to add data from the specified collection to the current collection with the help of addAll() method.
Using addAll() method of Collection Interface
The addAll() method of Collection interface adds data from a specified collection into current collection. It returns a boolean value, which is TRUE if the given collection is modified at the time of addition, else, it returns FALSE.
Syntax
Collection1.addAll(collection2);
Here, The collection 2 will be added at the end of collection 1.
Example
In the following Java program, we are adding and storing two collections in the third collection.
import java.util.*; public class Add { public static void main(String[] args) { ArrayList<String> cart1 = new ArrayList<String>(); cart1.add("Milk"); cart1.add("Bread"); cart1.add("Tea"); cart1.add("Butter"); System.out.println("Items in first cart: " + cart1); ArrayList<String> cart2= new ArrayList<String>(); cart2.add("Rice"); cart2.add("Flour"); cart2.add("Pulses"); cart2.add("Vegetables"); System.out.println("Items in second cart: " + cart2); ArrayList<String> bag = new ArrayList<String>(); bag.addAll(cart1); bag.addAll(cart2); System.out.println("Total items in Bag: " + bag); } }
Output:
Items in first cart: [Milk, Bread, Tea, Butter] Items in second cart: [Rice, Flour, Pulses, Vegetables] Total items in Bag: [Milk, Bread, Tea, Butter, Rice, Flour, Pulses, Vegetables]
Using addAll() method of List Interface
The List interface contains a different version of addAll() method. In List interface, it is an overloaded instance method. It inserts all elements of a collection into another starting at the specified index.
Syntax
Collection1.addAll(int index, Collection2);
The index is optional here, we only use it when we want to add second collection at a specific position. If we don't provide index position the collection2 will be added to the end of collection1.
Example
Again in the code given below, we have initialized two collections 'cart1' and 'cart2'. We are inserting elements of 'cart2' collection into 'cart1' at index 3.
import java.util.*; public class Add { public static void main(String[] args) { ArrayList<String> cart1 = new ArrayList<String>(); cart1.add("Milk"); cart1.add("Bread"); cart1.add("Tea"); cart1.add("Butter"); System.out.println("Items in first cart : " + cart1); ArrayList<String> cart2= new ArrayList<String>(); cart2.add("Rice"); cart2.add("Flour"); cart2.add("Pulses"); cart2.add("Vegetables"); System.out.println("Items in second cart : " + cart2); cart1.addAll(3, cart2); System.out.println("Total items after adding to current collection : " + cart1); } }
Output:
Items in first cart : [Milk, Bread, Tea, Butter] Items in second cart : [Rice, Flour, Pulses, Vegetables] Total items after adding to current collection : [Milk, Bread, Tea, Rice, Flour, Pulses, Vegetables, Butter]