A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/how-to-merge-two-linkedhashset-objects-in-java/ below:

How to Merge Two LinkedHashSet Objects in Java?

How to Merge Two LinkedHashSet Objects in Java?

Last Updated : 23 Jul, 2025

Use the addAll() method to merge two LinkedHashSet objects or append elements of one LinkedHashSet object to another LinkedHashSet object. Comparing with the set, addAll() method is used as Union. The addAll method adds all the elements of the specified collection to this set object.

Example:

Input : List1 = [1,2,3], List2 = [3, 4, 5]
Output: [1, 2, 3, 4, 5]

Input : List1 = ['a', 'b', 'c'], List2 = ['d']
Output: ['a', 'b', 'c', 'd']

Syntax:

boolean addAll(Collection<? extends E> collection)

Parameters: This is the collection containing elements to be added to this list.

Example:

Java
// How to merge two LinkedHashSet objects in Java?
import java.util.LinkedHashSet;
public class MergedLinkedHashSet {

    public static void main(String[] args)
    {

        LinkedHashSet<String> lhSetColors1
            = new LinkedHashSet<String>();

        lhSetColors1.add("yellow");
        lhSetColors1.add("white");
        lhSetColors1.add("black");

        LinkedHashSet<String> lhSetColors2
            = new LinkedHashSet<String>();

        lhSetColors2.add("yellow");
        lhSetColors2.add("red");
        lhSetColors2.add("green");

        /*
         * To merge two LinkedHashSet objects or
         * append LinkedHashSet object to another, use the
         * addAll method
         */
        lhSetColors1.addAll(lhSetColors2);

        System.out.println("Merged LinkedHashSet: "
                           + lhSetColors1);
    }
}

Output
Merged LinkedHashSet: [yellow, white, black, red, green]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4