
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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]
Advertisements