The HashSet.addAll() method in Java is used to add all elements from a specified collection to the HashSet. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
addAllMethod Syntax- Examples
- Adding Elements from Another Collection
- Adding Elements from a List to a HashSet
- Conclusion
Introduction
The HashSet.addAll() method is a member of the HashSet class in Java. It allows you to add all elements from a specified collection to the HashSet. If the specified collection contains elements that are already present in the HashSet, the duplicates are ignored, as HashSet only stores unique elements.
addAll Method Syntax
The syntax for the addAll method is as follows:
public boolean addAll(Collection<? extends E> c)
- The method takes a single parameter
cof typeCollection<? extends E>, which specifies the collection containing elements to be added to theHashSet. - The method returns a boolean value:
trueif theHashSetchanged as a result of the call.falseif theHashSetdid not change (i.e., all elements were already present).
Examples
Adding Elements from Another Collection
The addAll method can be used to add all elements from another HashSet to the current HashSet.
Example
import java.util.HashSet;
public class AddAllExample {
public static void main(String[] args) {
// Creating two HashSets of Strings
HashSet<String> languages1 = new HashSet<>();
HashSet<String> languages2 = new HashSet<>();
// Adding elements to the first HashSet
languages1.add("Java");
languages1.add("Python");
languages1.add("C");
// Adding elements to the second HashSet
languages2.add("C++");
languages2.add("JavaScript");
languages2.add("Python"); // Duplicate element
// Adding all elements from languages2 to languages1
boolean changed = languages1.addAll(languages2);
// Printing the result of addAll and the HashSet
System.out.println("Did languages1 change? " + changed);
System.out.println("languages1 after addAll: " + languages1);
}
}
Output:
Did languages1 change? true
languages1 after addAll: [Java, C++, C, JavaScript, Python]
Adding Elements from a List to a HashSet
The addAll method can also be used to add elements from a List to a HashSet.
Example
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
public class AddAllFromListExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> languages = new HashSet<>();
// Adding elements to the HashSet
languages.add("Java");
languages.add("Python");
// Creating a List of Strings
List<String> moreLanguages = new ArrayList<>();
moreLanguages.add("C");
moreLanguages.add("C++");
moreLanguages.add("JavaScript");
// Adding all elements from the List to the HashSet
boolean changed = languages.addAll(moreLanguages);
// Printing the result of addAll and the HashSet
System.out.println("Did languages change? " + changed);
System.out.println("languages after addAll: " + languages);
}
}
Output:
Did languages change? true
languages after addAll: [Java, C++, C, JavaScript, Python]
Conclusion
The HashSet.addAll() method in Java provides a way to add all elements from a specified collection to the HashSet. By understanding how to use this method, you can efficiently merge collections and ensure that the HashSet only contains unique elements. The method returns true if the HashSet was changed as a result of the operation, and false if all elements were already present in the HashSet.