We can use addAll() method to add all elements of a set to linkedhashset in java.
Syntax:
linkedHashSet.addAll(subSet);
Example:
package com.w3schools; import java.util.HashSet; import java.util.LinkedHashSet; public class Test { public static void main(String args[]){ //Create LinkedHashSet LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //Add elements to LinkedHashSet linkedHashSet.add("Jai"); linkedHashSet.add("Mahesh"); linkedHashSet.add("Vikas"); linkedHashSet.add("Sahil"); linkedHashSet.add("Sachin"); linkedHashSet.add("Vijay"); System.out.println("LinkedHashSet: " + linkedHashSet); HashSet<String> subSet = new HashSet<String>(); subSet.add("Lalit"); subSet.add("Ajay"); linkedHashSet.addAll(subSet); System.out.println("LinkedHashSet after modification: " + linkedHashSet); } } |
Output
LinkedHashSet: [Jai, Mahesh, Vikas, Sahil, Sachin, Vijay] LinkedHashSet after modification: [Jai, Mahesh, Vikas, Sahil, Sachin, Vijay, Lalit, Ajay] |