In this example we will show how to remove a specified object or element contained in Java LinkedHashSet object with remove method.
Source Code
package com.beginner.examples;
import java.util.LinkedHashSet;
public class RemoveSpecifiedElementFromLinkedHashSet {
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 C, linkedHashSet contains : " + linkedHashSet);
/*
* To remove an element from Java LinkedHashSet object use
* remove(Object o) method.
*/
linkedHashSet.remove("C");
System.out.println("After remove C, linkedHashSet contains : " + linkedHashSet);
}
}
Output:
Before remove C, linkedHashSet contains : [A, B, C, D]
After remove C, linkedHashSet contains : [A, B, D]
References
Imported packages in Java documentation: