/** * @author lautturi.com * Java example: add new element to LinkedHashSet in java */ import java.util.*; public class Lautturi { public static void main(String[] args) { LinkedHashSet<Integer> hashset = new LinkedHashSet<>(); // Add elements to LinkedHashSet using add() method hashset.add(1); hashset.add(2); hashset.add(3); System.out.println("LinkedHashSet: " + hashset); LinkedHashSet<Integer> hashset2 = new LinkedHashSet<>(); // Add elements to LinkedHashSet using addAll() method hashset2.addAll(hashset); hashset2.add(4); System.out.println("New LinkedHashSet: " + hashset2); } }
output:
LinkedHashSet: [1, 2, 3] New LinkedHashSet: [1, 2, 3, 4]