Open In App

HashSet remove() Method in Java

Last Updated : 05 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

HashSet remove() method is used to remove a particular element from a HashSet. Note that it is only after JDK version 1.2 and ahead, and will throw compilation errors before in version JDK 1 and JDK1.1. 

Note: This method returns true if the specified element is present in the HashSet otherwise it returns boolean false.

Syntax: 

HashSet.remove(Object O)

Parameters: The parameter O is of the type of HashSet and specifies the element to be removed from the HashSet.

Return Value: Boolean true and false 

Example 1:

Java




// Java code to illustrate
// HashSet.remove() method
// over String Elements
 
// Importing required classes
import java.util.*;
 
// Main class
// HashSet demo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty HashSet
        // Declaring object of string type
        HashSet<String> set = new HashSet<String>();
 
        // Adding custom input elements into the Set
        // using add() method
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("For");
        set.add("Geeks");
 
        // Displaying the HashSet(object elements)
        System.out.println("HashSet: " + set);
 
        // Removing elements
        // using remove() method
        set.remove("Geeks");
        set.remove("For");
        set.remove("Welcome");
 
        // Now displaying the HashSet after removal
        // of elements from it
        System.out.println(
            "HashSet after removing elements: " + set);
    }
}


Output

HashSet: [Geeks, For, Welcome, To]
HashSet after removing elements: [To]

Example 2:

Java




// Java code to illustrate remove()
// method of Hashset class
// over Integer Elements
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty HashSet
        // Declaring object of integer type
        HashSet<Integer> set = new HashSet<Integer>();
 
        // Adding custom input elements into the Set
        // using add() method
        set.add(5);
        set.add(3);
        set.add(1);
        set.add(4);
        set.add(3);
 
        // Displaying the HashSet(object elements)
        System.out.println("HashSet: " + set);
 
        // Removing elements
        // using remove() method
        set.remove(3);
        set.remove(1);
 
        // Now displaying the HashSet after removal
        // of elements from it
        System.out.println(
            "HashSet after removing elements: " + set);
    }
}


Output

HashSet: [1, 3, 4, 5]
HashSet after removing elements: [4, 5]


Previous Article
Next Article

Similar Reads

How to Copy or Append HashSet to Another HashSet in Java?
HashSet is used to store distinct values in Java. HashSet stores the elements in random order, so there is no guarantee of the elements' order. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. We can copy or append a HashSet to another HashSet. There is a couple of ways to copy HashSet or
4 min read
HashSet contains() Method in Java
The Java.util.HashSet.contains() method is used to check whether a specific element is present in the HashSet or not. So basically it is used to check if a Set contains any particular element. Syntax: Hash_Set.contains(Object element) Parameters: The parameter element is of the type of HashSet. This is the element that needs to be tested if it is p
2 min read
HashSet clear() Method in Java
The Java.util.HashSet.clear() method is used to remove all the elements from a HashSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing HashSet. Syntax: Hash_Set.clear() Parameters: The method does not take any parameter R
1 min read
HashSet isEmpty() Method in Java
The Java.util.HashSet.isEmpty() method is used to check if a HashSet is empty or not. It returns True if the HashSet is empty otherwise it returns False. Syntax: Hash_Set.isEmpty() Parameters: This method does not take any parameter Return Value: The function returns True if the set is empty else returns False. Below program illustrate the Java.uti
1 min read
HashSet iterator() Method in Java
The Java.util.HashSet.iterator() method is used to return an iterator of the same elements as the hash set. The elements are returned in random order from what present in the hash set. Syntax: Iterator iterate_value = Hash_Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of th
1 min read
HashSet size() Method in Java
The Java.util.HashSet.size() method is used to get the size of the HashSet or the number of elements present in the HashSet. Syntax: Hash_Set.size() Parameters: This method does not takes any parameter. Return Value: The method returns the size or the number of elements present in the HashSet. Below program illustrate the Java.util.HashSet.size() m
1 min read
HashSet add() Method in Java
The Java.util.HashSet.add() method in Java HashSet is used to add a specific element into a HashSet. This method will add the element only if the specified element is not present in the HashSet else the function will return False if the element is already present in the HashSet. Syntax: Hash_Set.add(Object element) Parameters: The parameter element
1 min read
HashSet clone() Method in Java
The Java.util.HashSet.clone() method is used to return a shallow copy of the mentioned hash set. It just creates a copy of the set. Syntax: Hash_Set.clone() Parameters: The method does not take any parameters. Return Value: The method just returns a copy of the HashSet. Below program illustrate the Java.util.HashSet.clone() method: // Java code to
1 min read
HashSet spliterator() method in Java
The spliterator() method of HashSet returns a Spliterator with the same elements as HashSet. The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means HashSet at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliter
2 min read
HashSet hashCode() method in Java with Example
The hashCode() method of HashSet in Java is used to get the hashCode value for this instance of the HashSet. It returns an integer value which is the hashCode value for this instance of the HashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: The method returns an integer value which is the hashCode value for
2 min read