Java HashSet.remove() – Examples

In this tutorial, we will learn about the Java HashSet.remove() method, and learn how to use this method to remove an object/element from this HashSet, with the help of examples.

remove(Object o)

HashSet.remove() removes the specified object/element from this HashSet if it is present.

If the element is present and removed by remove() method, then the method returns true.

If the element is not present, then the remove() method returns false.

Syntax

The syntax of remove() method is

HashSet.remove(Object obj)

where

ParameterDescription
objThe object/element to be removed from this HashSet.

Returns

This method returns boolean value.

Example 1 – remove()

In this example, we will define and initialize a HashSet of Strings. We shall then use remove() method on this HashSet to remove the String object "c".

Java Program

import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("a");
		hashSet.add("b");
		hashSet.add("c");
		hashSet.add("d");
		System.out.println("Original HashSet       : " + hashSet);
		
		String object = "c";
		boolean result = hashSet.remove(object);
		System.out.println("Is the object removed? " + result);
		System.out.println("HashSet after remove() : " + hashSet);
    }  
}

Output

Original HashSet       : [a, b, c, d]
Is the object removed? true
HashSet after remove() : [a, b, d]

Example 2 – remove() – Element not present in HashSet

In this example, we will try to remove an object from the HashSet, in which case the element is not present in this HashSet.

Java Program

import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("a");
		hashSet.add("b");
		hashSet.add("c");
		hashSet.add("d");
		System.out.println("Original HashSet       : " + hashSet);
		
		String object = "m";
		boolean result = hashSet.remove(object);
		System.out.println("Is the object removed? " + result);
		System.out.println("HashSet after remove() : " + hashSet);
    }  
}

Output

Original HashSet       : [a, b, c, d]
Is the object removed? false
HashSet after remove() : [a, b, c, d]

Example 3 – remove() – User-defined Objects

In this example, we will define and initialize a HashSet of type Car. We shall then use remove() method on this HashSet to remove a specific Car object.

Java Program

import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		HashSet<Car> hashSet = new HashSet<Car>();
		hashSet.add(car1);
		hashSet.add(car2);
		hashSet.add(car3);
		System.out.println("Original HashSet       : " + hashSet);
		
		Car object = car2;
		boolean result = hashSet.remove(object);
		System.out.println("Is object removed? " + result);
		System.out.println("HashSet after remove() : " + hashSet);
    }  
}

class Car {
	int id;
	String name;
	
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return id + "-" + name;
	}
}

Output

Original HashSet       : [1-Tesla, 3-Toyota, 2-BMW]
Is object removed? true
HashSet after remove() : [1-Tesla, 3-Toyota]

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashSet.remove() method, and also learnt how to use this method with the help of Java examples.