How To Remove Element 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]

Leave a Reply

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

4 + 4 =