Java LinkedHashSet addAll() Method

The LinkedHashSet.addAll() method in Java is used to add all the elements from a specified collection to the LinkedHashSet.

Table of Contents

  1. Introduction
  2. addAll Method Syntax
  3. Examples
    • Adding All Elements from Another Collection to LinkedHashSet
    • Handling Duplicate Elements
  4. Conclusion

Introduction

The LinkedHashSet.addAll() method is a member of the LinkedHashSet class in Java. It allows you to add all the elements from a specified collection to the LinkedHashSet. If the collection contains elements that are already present in the set, the duplicates are ignored.

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 c of type Collection<? extends E>, which represents the collection containing elements to be added to the LinkedHashSet.
  • The method returns a boolean value:
    • true if the LinkedHashSet changed as a result of the call.
    • false if the LinkedHashSet did not change (i.e., all elements were already present).

Examples

Adding All Elements from Another Collection to LinkedHashSet

The addAll method can be used to add all elements from another collection to a LinkedHashSet.

Example

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class AddAllExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding initial elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");

        // Creating a list of new animals to be added
        ArrayList<String> newAnimals = new ArrayList<>();
        newAnimals.add("Elephant");
        newAnimals.add("Giraffe");

        // Adding all elements from the list to the LinkedHashSet
        animals.addAll(newAnimals);

        // Printing the LinkedHashSet
        System.out.println("LinkedHashSet after addAll: " + animals);
    }
}

Output:

LinkedHashSet after addAll: [Lion, Tiger, Elephant, Giraffe]

Handling Duplicate Elements

The addAll method ignores duplicate elements already present in the LinkedHashSet.

Example

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class AddAllDuplicatesExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding initial elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");

        // Creating a list with some duplicate elements
        ArrayList<String> newAnimals = new ArrayList<>();
        newAnimals.add("Tiger");
        newAnimals.add("Elephant");

        // Adding all elements from the list to the LinkedHashSet
        boolean isChanged = animals.addAll(newAnimals);

        // Printing the result of addAll and the LinkedHashSet
        System.out.println("Was the LinkedHashSet changed? " + isChanged);
        System.out.println("LinkedHashSet after addAll: " + animals);
    }
}

Output:

Was the LinkedHashSet changed? true
LinkedHashSet after addAll: [Lion, Tiger, Elephant]

Conclusion

The LinkedHashSet.addAll() method in Java provides a way to add all elements from another collection to a LinkedHashSet. By understanding how to use this method, you can efficiently manage collections and ensure that all elements from a specified collection are added to your LinkedHashSet, while automatically handling duplicates. This method is useful for combining collections and managing data in a structured manner in your Java applications.

Comments