How to copy the elements of LinkedHashSet to an array

How to copy the elements of LinkedHashSet to an array

Previous Home Next

 

This example will shows us how to copy the elements of LinkedHashSet to an Array object.

To copy elements of LinkedHashSet to an Array object we object[] to Array () method. 
 object[] to Array () method.will copy all the elements of Set to the Array object.


 

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

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



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());
Object[] objArray = Lhst.toArray();
System.out.println("The elemnts of set are copied to the array object:");
for(int index=0; index<objArray.length ;index++)
System.out.println(objArray[index]);



}

}

 The LinkedHashSet contains the elements :[10, 11, 12, 13, 14, 15, 16]
 The size of the LinkedHashSet is:7
The elemnts of set are copied to the array object:
10
11
12
13
14
15
16

Previous Home Next