How to remove all the elements of the LinkedHashSet?
Previous | Home | Next |
This program will shows us how to remove all the elements from the LinkedHashSet.
We use removeall(Collection c) method to remove the elements from the Set. it will remove the all the elements containing in a set.
This method removeall(collection c) use iterator it will checks the set till it became empty. iterator's hasnext() and next () method are used to do so.
package Example;
import java.util.LinkedHashSet;
import java .util.Iterator;
/**
* @author R4R
*
*/
public class IterateElements {
/**
* @param args
*/
public static void main(String[] args) {
LinkedHashSet Lhst = new LinkedHashSet();
Lhst.add("10");
Lhst.add("11");
Lhst.add("12");
Lhst.add("13");
Lhst.add("14");
Lhst.add("15");
Lhst.add("16");
System.out.println(" The LinkedHashSet contains the elements :"+Lhst);
Object obj1=Lhst.removeAll(Lhst);
Iterator itr = Lhst.iterator();
System.out.println("All the elements from the LinkedHashSet are removed :" + obj1);
while(itr.hasNext())
System.out.println(itr.next());
// TODO Auto-generated method stub
}
}
The LinkedHashSet contains the elements :[10, 11, 12, 13, 14, 15, 16]
All the elements from the LinkedHashSet are removed :true
Previous | Home | Next |