The LinkedList.addAll()
method in Java is used to add all elements from a specified collection to a LinkedList
. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality, including its overloaded methods.
Table of Contents
- Introduction
addAll
Method Syntax- Examples
- Adding All Elements from a Collection
- Adding All Elements from a Collection at a Specific Index
- Handling Null Collections
- Conclusion
Introduction
The LinkedList.addAll()
method is a member of the LinkedList
class in Java. It allows you to add all elements from a specified collection to a LinkedList
, either at the end of the list or at a specific position. This method is particularly useful for combining collections or extending a LinkedList
with multiple elements at once.
addAll Method Syntax
The syntax for the addAll
method is as follows:
Adding All Elements to the End
public boolean addAll(Collection<? extends E> c)
- c: The collection containing elements to be added to the end of the
LinkedList
.
Adding All Elements at a Specific Index
public boolean addAll(int index, Collection<? extends E> c)
- index: The index at which the specified collection is to be inserted.
- c: The collection containing elements to be added.
Examples
Adding All Elements from a Collection
The addAll
method can be used to add all elements from a collection to the end of a LinkedList
.
Example
import java.util.LinkedList;
import java.util.ArrayList;
public class AddAllExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
// Creating another collection (ArrayList) of Strings
ArrayList<String> moreFruits = new ArrayList<>();
moreFruits.add("Orange");
moreFruits.add("Grapes");
// Adding all elements from the ArrayList to the LinkedList
list.addAll(moreFruits);
// Printing the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
LinkedList: [Apple, Banana, Orange, Grapes]
Adding All Elements from a Collection at a Specific Index
You can add all elements from a collection at a specific position in the LinkedList
using the addAll
method with an index.
Example
import java.util.LinkedList;
import java.util.ArrayList;
public class AddAllExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
// Creating another collection (ArrayList) of Strings
ArrayList<String> moreFruits = new ArrayList<>();
moreFruits.add("Orange");
moreFruits.add("Grapes");
// Adding all elements from the ArrayList to the LinkedList at index 1
list.addAll(1, moreFruits);
// Printing the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
LinkedList: [Apple, Orange, Grapes, Banana]
Handling Null Collections
The addAll
method will throw a NullPointerException
if the specified collection is null.
Example
import java.util.LinkedList;
public class AddAllExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
try {
// Attempting to add elements from a null collection
list.addAll(null);
} catch (NullPointerException e) {
System.out.println("Exception: " + e);
}
// Printing the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
Exception: java.lang.NullPointerException
LinkedList: [Apple, Banana]
Conclusion
The LinkedList.addAll()
method in Java provides a convenient way to add all elements from a specified collection to a LinkedList
. By understanding how to use this method and its overloaded versions, you can efficiently manage and combine collections of objects in your Java applications. Whether you are adding all elements to the end of the list or inserting them at specific positions, the addAll
method offers a flexible solution for these tasks.