How to clear the LinkedHashSet?

How to clear the LinkedHashSet?

Previous Home Next

 

This Example will shows us how to clear the LinkedHashSet .Or we can say to make it empty.

This program use clear() method of  LinkedHashSet class.It will Removes all of the elements from this set. The set will be empty after this call 
returns. 
 public void clear () method is used here to make the LinkedHashSet Empty.and boolean isEmpty() is used to check whether it is empty or not . it will shows the result in true/false

After populated the LinkedHashSet .use  void clear() method to make it empty.the check whether it will empty or not by using the isEmpty ().

 

/**
*
*/
package Example;
import java.util.LinkedHashSet;
import java .util.Iterator;

/**
* @author R4R
*
*/
public class ClearElements {





/**
* @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);

Lhst.clear();
System.out.println(" All the elements from the LinkedhashSet is removed:" + Lhst.isEmpty());



// TODO Auto-generated method stub


}

}


 The LinkedHashSet contains the elements :[10, 11, 12, 13, 14, 15, 16]
 All the elements from the LinkedhashSet is removed:true

Previous Home Next