![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Java Program to remove all elements from a set in Java
To remove all elements from a set, use the clear() method.
Here is our set −
HashSet <String> set1 = new HashSet <String>(); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");
Now, let us remove all the elements −
set1.clear();
The following is an example to remove all elements from a set −
Example
import java.util.*; public class Demo { public static void main(String args[]) { HashSet <String> set1 = new HashSet <String>(); set1.add("Mat"); set1.add("Sat"); set1.add("Cat"); System.out.println("Set = "+ set1); set1.clear(); System.out.println("Set after removing all the elements (blank) = "+ set1); } }
Output
Set = [Mat, Sat, Cat] Set after removing all the elements (blank) = []
Advertisements