- Trending Categories
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) = []
- Related Articles
- Remove all elements from a HashSet in Java
- Remove all elements from TreeSet in Java
- Remove all elements from Java NavigableMap
- Remove all elements from Java LinkedHashSet
- Java program to remove items from Set
- Remove all elements from the ArrayList in Java
- Java program to remove duplicates elements from a List
- Java Program to Remove All Whitespaces from a String
- Java Program to Remove elements from the LinkedList
- Java Program to Remove duplicate elements from ArrayList
- Java Program to add and remove elements from a set which maintains the insertion order
- Remove all elements in Java IdentityHashMap
- Java Program to remove all white spaces from a String.
- How to remove all the elements from a set in javascript?
- Copy all the elements from one set to another in Java

Advertisements