Java Program to Add the Data from the Specified Collection in the Current Collection
Last Updated :
05 Oct, 2021
The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a true value if the adding of the collection is successful after calling the method else it returns a false value.
We can use the addAll() method in two ways namely:
- The static method
- The instance method
Let there be 2 lists.
The static method Declaration
Collections.addAll(List1,List2)
The instance method Declaration
Let the index where List2 needs to be inserted in List1 is 1.
List1.addAll(1, List2);
The addAll() method always throws the following exceptions:
- NullPointerException: This exception is thrown if the specified or the current collection has null values.
- IllegalArgumentException: If the arguments of the specified collection have values that prevent them from being added to the current collection, this exception is thrown.
Example 1:
Input: boolean b = List1.addAll(large,extra-large)
If the appending is successful
Output: true
If the appending is unsuccessful
Output: false
Example 2:
Input: Collections.addAll(List1,List2)
Output: List1 = [small,medium,large,extra-large]
Approach used:
- Initializing the two collections.
- Using the addAll() method to append the lists.
- Checking the boolean value of the method call.
- Printing the collections' final values.
Below is the implementation of the above approach:
Java
// Java Program to Add the Data from the Specified
// Collection in the Current Collection
import java.util.*;
public class AddCollections {
public static void main(String[] args)
{
// Creating object of a list which is our current
// collection
ArrayList<Integer> list = new ArrayList<Integer>();
// Adding values to the initial list
list.add(1);
list.add(2);
list.add(3);
// Printing the initial list
System.out.println(
"Initial collection value of list: " + list);
// creating object of the specified list.
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(4);
list2.add(5);
list2.add(6);
// Printing the initial list
System.out.println(
"Initial collection value of list2: " + list2);
// returns true if addition is successful else
// false
// adding data from the specified collection in
// the current collection at a specified position
boolean b = list.addAll(2, list2);
// printing the boolean result
System.out.println("Boolean Result: " + b);
// printing the final list with the new values added
System.out.println(
"Final collection value of list: " + list);
// creating an object for a different collection
Integer[] arr = new Integer[4];
// Initializing the array
arr[0] = 9;
arr[1] = 8;
arr[2] = 7;
arr[3] = 6;
// Adding array elements to list2
Collections.addAll(list2, arr);
// Printing the new List2
System.out.println(
"Final collection value of list2: " + list2);
}
}
OutputInitial collection value of list: [1, 2, 3]
Initial collection value of list2: [4, 5, 6]
Boolean Result: true
Final collection value of list: [1, 2, 4, 5, 6, 3]
Final collection value of list2: [4, 5, 6, 9, 8, 7, 6]
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read