- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove specified element from HashSet in Java
To remove specified element from HashSet, use the remove() method.
First, declare a HashSet and add elements −
Set<Integer> hs = new HashSet<Integer>(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88);
Let’s say you need to remove element 39. For that, use the remove() method −
hs.remove(39);
The following is an example to remove specified element from HashSet −
Example
import java.util.*; public class Demo { public static void main(String args[]) { Set<Integer> hs = new HashSet<Integer>(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88); System.out.println("Elements = "+hs); // remove specific elements hs.remove(39); System.out.println("Updated Elements = "+hs); } }
Output
Elements = [81, 67, 20, 39, 87, 88, 79] Updated Elements = [81, 67, 20, 87, 88, 79]
- Related Articles
- Remove the specified element from a HashSet in C#
- Remove single element from a HashSet in Java
- Remove specified element from Java LinkedHashSet
- Remove specified element from TreeSet in Java
- Remove all elements from a HashSet in Java
- Check if a HashSet contains the specified element in C#
- Remove duplicate elements in Java with HashSet
- Remove the element with the specified key from a SortedList in C#
- Remove the element with the specified key from the Hashtable in C#
- How to remove data from hashset in Android?
- Remove all elements from a HashSet in C#
- Remove an element from IdentityHashMap in Java
- Find maximum element of HashSet in Java
- Find minimum element of HashSet in Java
- How to remove element from the specified index of the List in C#?

Advertisements