How to Merge Two LinkedHashSet Objects in Java?
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.
Merged LinkedHashSet: [yellow, white, black, red, green]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.