HashSet remove() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The HashSet remove() method in Java is used to remove a specific element from the set if it is present.Note: HashSet and the remove() were introduced in JDK 1.2 as part of the Collections Framework and are not available in earlier versions of Java (JDK 1.0 and JDK 1.1).Example 1: Here, the remove() method is used to remove a specified element from the set. Java // Java Program to demonstrates the working of remove() import java.util.*; public class Geeks { public static void main(String args[]) { // Create a HashSet HashSet<Integer> s = new HashSet<>(); // add elements into a HashSet s.add(1); s.add(2); s.add(3); s.add(4); s.add(5); System.out.println("Original HashSet: " + s); s.remove(2); // Now displaying the HashSet after removal System.out.println( "HashSet after removing elements: " + s); } } OutputOriginal HashSet: [1, 2, 3, 4, 5] HashSet after removing elements: [1, 3, 4, 5] Syntax of HashSet remove() Methodpublic boolean remove(Object o)Parameter: The object to be removed from the HashSet.Return Type: This method returns true if the element is successfully removed; otherwise, it returns false.Example 2: This example demonstrates that the remove() method in a HashSet returns a boolean value indicating whether the specified element was successfully removed. Java // To demonstrates that remove() method return boolean value import java.util.*; public class Geeks { public static void main(String args[]) { // Create a HashSet HashSet<Integer> s = new HashSet<>(); // Add elements into a HashSet s.add(1); s.add(2); s.add(3); s.add(4); s.add(5); boolean b = s.remove(2); System.out.println("Was 2 removed? " +b); boolean n = s.remove(10); System.out.println("Was 10 removed? " +n); } } OutputWas 2 removed? true Was 10 removed? false Comment More info C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-hashset +2 More Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like