HashSet Remove Method in Java

In this tutorial we will learn about HashSet remove method in java .  Java HashSet remove method use to remove element. We will see different method to remove element from HashSet using hashset remove() method .

Syntax :

public boolean remove(Object o) {

}

Removes the specified element from this HashSet if it is present. More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this HashSet contains such an element.

 Parameter : The object/element to be removed from this HashSet.                                             Return Value : It returns boolean value .

Example 1 : HashSet Remove() method – Element Present

In this example , We will create HashSet of String and add elements into HashSet using add() method. We shall then use remove method to remove element php from HashSet .

public class HashSetRemove {

	public static void main(String[] args) {
		// Constructs a new, empty set
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("java");
		hashSet.add("php");
		hashSet.add("android");

		// display the element
		System.out.println("element in hashset =" + hashSet);

		/* * Removes the specified element from  set 
		   if it is present */
		boolean isElementRemoved =  hashSet.remove("php");
		
	 System.out.println(" is Element removed From HashSet ? "+ isElementRemoved );

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

}

Output:

element in hashset =[java, android, php]
 is Element removed From HashSet ? true
After Remove element hashset is =[java, android]

Example 2 : HashSet Remove() method – Element not Present

In this example , We will initialize HashSet of String . Then we shall  use remove method to remove element  from HashSet. But element is not present in HashSet , So remove() method return false.

import java.util.HashSet;

public class HashSetRemove1 {
	
	public static void main(String[] args) {
		// Constructs a new, empty set
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("java");
		hashSet.add("php");
		hashSet.add("android");

		// display the element
		System.out.println("element in hashset =" + hashSet);

	boolean isElementRemoved =  hashSet.remove("html");
		
	System.out.println(" is Element removed From HashSet ? "+ isElementRemoved );

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

	}	
}

Output:

element in hashset =[java, android, php]
 is Element removed From HashSet ? false
After Remove element hashset is =[java, android, php]

Example 2 : HashSet Remove() method – Integer Elements

In this example we will initialize HashSet Of Integer type . Then we shall remove element for HashSet using  remove() method.

import java.util.HashSet;

public class HashSetRemove2 {

	public static void main(String[] args) {

		// Constructs a new, empty set
		HashSet<Integer> hashSet = new HashSet<Integer>();

		// add element in it
		hashSet.add(10);
		hashSet.add(20);
		hashSet.add(30);

		// display the element
		System.out.println("element in hashset =" + hashSet);

		boolean isElementRemoved = hashSet.remove(30);

		System.out.println(" is Element removed From HashSet ? "
				+ isElementRemoved);

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

	}

}

Output:

element in hashset =[20, 10, 30]
 is Element removed From HashSet ? true
After Remove element hashset is =[20, 10]

In this tutorial we leaned about java hashset remove() method . We have see different example for remove() method of HashSet .You can see more java HashSet Example f or practice .

A Guide to HashSet                                                                                                              Java Doc – HahSet

Leave a Reply

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

29 − = 22