.addAll()
The .addAll()
method is declared in the Set
interface and is implemented in the HashSet
class. It is used to add all of the elements in the specified collection
to this set if they’re not already present. The function returns a boolean value true
if the elements are added successfully, and false
if not.
Syntax
The .addAll()
method can be called on a HashSet
instance and it requires one parameter, the collection which we need to add in the set:
hashSet.addAll(Collection<? extends E> c);
Collection<? extends E> c
: Here c
is a collection of type ? extends E
. The method can take any collection of elements that are a subtype of E. This means that you can add elements of a subclass of E to the Set, but you cannot add elements of a superclass of E.
Example
In the example below, the .addAll()
method is used to add elements of shoppingList1
and shoppingList2
in the combinedShoppingList
set:
import java.util.HashSet;import java.util.Set;public class GroceryShopping {public static void main(String[] args) {// Creating two sets representing grocery shopping listsSet<String> shoppingList1 = new HashSet<>();Set<String> shoppingList2 = new HashSet<>();// Adding items to the first shopping listshoppingList1.add("Apples");shoppingList1.add("Bananas");shoppingList1.add("Milk");// Adding items to the second shopping list, including a common item with shoppingList1shoppingList2.add("Grapes");shoppingList2.add("Bananas"); // Common itemshoppingList2.add("Bread");// Creating a new set to combine items from both shopping listsSet<String> combinedShoppingList = new HashSet<>();// Using addAll() to combine items from shoppingList1 and shoppingList2combinedShoppingList.addAll(shoppingList1);combinedShoppingList.addAll(shoppingList2);// Displaying the contents of the combined shopping listSystem.out.println("Combined Shopping List: " + combinedShoppingList);}}
The above example will give the following output:
Combined Shopping List: [Bread, Grapes, Bananas, Apples, Milk]
In the above example, both sets contain Bananas
, however, the combinedShoppingList
set contains it only once.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.