1.Basic java example program to remove all elements in linkedhashset
Output:
- clear() This method used to remove all elements from Linkedhashset.
- package com.removeelementLinkedhashset;
- import java.util.LinkedHashSet;
- import java.util.Iterator;
- public class LinkedHashsetExample{
- public static void main(String[] args) {
- LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
- linkedhashset.add("Java Interview Questions");
- linkedhashset.add("Java interview program");
- linkedhashset.add("Concept and example program");
- linkedhashset.add("Concept and interview questions");
- linkedhashset.add("Java Quiz");
- System.out.println("LinkedHashSet before removal : " + linkedhashset);
- boolean blnRemoved = linkedhashset.remove("Java Quiz");
- System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
- System.out.println("LinkedHashSet after removal : ");
- Iterator it=linkedhashset.iterator();
- while(it.hasNext()){
- System.out.println(it.next());
- }
- linkedhashset.clear();
- System.out.println("LinkedHashSet after removal all elements");
- }
- }
Output:
- LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
- and example program, Concept and interview questions, Java Quiz]
- Was Java Quiz removed from LinkedHashSet ? true
- LinkedHashSet after removal :
- Java Interview Questions
- Java interview program
- Concept and example program
- Concept and interview questions
- LinkedHashSet after removal all elements :[]
Bagikan
Remove all elements LinkedHashSet example
4/
5
Oleh
Kris Kimcil