In this tutorial we will see how to remove element from set java . We can remove element from Hashset in Java with the help of remove() method of hahset.
Syntax :
public boolean remove(Object o) {
}
Removes the specified element from this set if it is present. More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element.
Parameter : o object to be removed from this set, if present Return Value : true if the set contained the specified element
Let’s example for remove method of hashset
import java.util.HashSet;
import java.util.Set;
public class RemoveElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("Before Remove element hashset is = "+hs);
/*
* Removes the specified element from set
* if it is present
*/
hs.remove("php");
System.out.println("After Remove element hashset is ="+hs);
}
}
Output:
Before Remove element hashset is = [javascript, php, html, java, mysql]
After Remove element hashset is =[javascript, html, java, mysql]
A Guide to HashSet Java Doc – HahSet