Skip to content
Related Articles

Related Articles

How to Merge Two LinkedHashSet Objects in Java?

Improve Article
Save Article
  • Last Updated : 11 Dec, 2020
Improve Article
Save Article

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.


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']


boolean addAll(Collection<? extends E> collection)

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



// 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>();
        LinkedHashSet<String> lhSetColors2
            = new LinkedHashSet<String>();
         * To merge two LinkedHashSet objects or
         * append LinkedHashSet object to another, use the
         * addAll method
        System.out.println("Merged LinkedHashSet: "
                           + lhSetColors1);


Merged LinkedHashSet: [yellow, white, black, red, green]
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!