- 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 all elements from Java LinkedHashSet
To remove all the elements from LinkedHashSet in Java, use the clear() method.
The following is an example to declare LinkedHashSet and add elements to it −
LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);
Use the clear() method to remove all elements −
hashSet.clear();
The following is an example −
Example
import java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60); System.out.println("LinkedHashSet..."); System.out.println(hashSet); // cleared the set hashSet.clear(); System.out.println("
Updated LinkedHashSet..."); System.out.println(hashSet); } }
Output
LinkedHashSet... [10, 20, 30, 40, 50, 60] Updated LinkedHashSet... []
- Related Articles
- Remove specified element from Java LinkedHashSet
- Remove all elements from Java NavigableMap
- Remove all elements from TreeSet in Java
- Copy all elements of Java LinkedHashSet to an Object Array
- Remove all elements from a HashSet in Java
- Remove all elements from the ArrayList in Java
- Iterate through elements of Java LinkedHashSet
- Java Program to remove all elements from a set in Java
- Remove all elements in Java IdentityHashMap
- Fetch key-valuepair from Java LinkedHashSet
- Remove all elements from OrderedDictionary in C#
- Remove all elements from a HashSet in C#
- Remove all elements from a SortedList in C#
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#

Advertisements