- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces. We can perform various operations on collection like adding, removing, iterating, searching and retrieving objects.
In this article, we will discuss a java program to add data from the specified collection in the current collection with the help of addAll() and add() method.
addAll() − It add data from a specified collection into current collection. Its return type is Boolean, i.e., if specified collection gets added to the current collection it returns true otherwise false. There are two ways of using the addAll() method.
add() − It adds a specific element to a current collection. We are required to import Collections and ArrayList classes available in java.util package.
Approach1: Using addAll() as Static method
All the collection interface and their methods are present in the ‘java.util’ package. Therefore, to work with it we need to import this package using following command −
import java.util.*;
Here, * is representing that we are importing all the classes and methods present in this package.
Syntax
Collections.addAll(collection1, collection2);
Here, The collection2 will be added at the end of collection1.
Example
List1 = [ 1, 2, 3 ]
List2 = [ A, B, C ]
After grouping them: [ 1, 2, 3, A, B, C ]
Example
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]
In the above code, we have initialized two collections ‘cart1’ and ‘cart2’. We are adding and storing both collections in third collection named ‘bag’.
Approach 2: Using addAll() as Instance method
It works with collection interface only ( like arraylist, set, queue).
Example
List1 = [ 1, 2, 3 ]
List2 = [ A, B, C ]
Let’s assume we have given the value of index 1. Then, list2 will gets added before value 2 of list1.
After grouping them: [ 1, A, B, C, 2, 3 ]
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
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]
Again in the above code, we have initialized two collections ‘cart1’ and ‘cart2’. We have added ‘cart2’ collection into ‘cart1’ at position 3. If we didn’t mention that index value, it will get added to the end of cart1.
Conclusion
In this article, we have understood the use of addAll() method in making a java program to add data from the specified collection in the current collection. The addAll() method is used in two ways one as a static method and the other as an instance method. We have understood both ways of doing it. Importing the class Collection and ArrayList is necessary for both of the java programs.