How to iterate through the elements of LinkedHashSet?

How to iterate through the elements of LinkedHashSet?

Previous Home Next

 

This example will shows us how to iterate through the elements of the LinkedHashSet.

this program use iterator method of iterator class to iterate through the elements.This will display the result in ascending order. 
 Iterator method use Has used hasnext () method and next() method to iterate in the list.


 


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);
Lhst.size();
System.out.println(" The size of the LinkedHashSet is:" + Lhst.size());

Iterator itr = Lhst.iterator();

/
while(itr.hasNext())

System.out.println("elements after iteration in ascending order are:" +itr.next());
// TODO Auto-generated method stub


}

}

 The LinkedHashSet contains the elements :[10, 11, 12, 13, 14, 15, 16]
 The size of the LinkedHashSet is:7
elements after iteration in ascending order  are:10
elements after iteration in ascending order  are:11
elements after iteration in ascending order  are:12
elements after iteration in ascending order  are:13
elements after iteration in ascending order  are:14
elements after iteration in ascending order  are:15
elements after iteration in ascending order  are:16

Previous Home Next