How To Remove Element of Hashset

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>();

//print hashset
System.out.println("Before Remove element hashset is = "+hs);

* Removes the specified element from  set
* if it is present

System.out.println("After Remove element hashset is ="+hs);




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


Leave a Reply

Your email address will not be published. Required fields are marked *

− 2 = 7