The example aims to remove all elements contained in Java LinkedHashSet or clear LinkedHashSet object by removeAll method.
Source Code
package com.beginner.examples;
import java.util.LinkedHashSet;
public class RemoveAllElementsFromLinkedHashSet {
public static void main(String[] args) {
//create LinkedHashSet.
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add("A");
linkedHashSet.add("B");
linkedHashSet.add("C");
linkedHashSet.add("D");
System.out.println("Before remove, linkedHashSet contains : " + linkedHashSet);
/*
* Use clear method to To remove all elements.
*/
linkedHashSet.clear();
System.out.println("After remove, linkedHashSet contains : " + linkedHashSet);
}
}
Output:
Before remove, linkedHashSet contains : [A, B, C, D]
After remove, linkedHashSet contains : []
References
Imported packages in Java documentation: