Copy all elements of LinkedHashSet to an Object Array - Java Collection Framework

Java examples for Collection Framework:LinkedHashSet

Description

Copy all elements of LinkedHashSet to an Object Array

Demo Code

 
import java.util.LinkedHashSet;
 
public class Main {
 
  public static void main(String[] args) {
    LinkedHashSet lhashSet = new LinkedHashSet();
   //w w w  .  ja v a2s. c  o m
    //add elements to LinkedHashSet object
    lhashSet.add(new Integer("1"));
    lhashSet.add(new Integer("2"));
    lhashSet.add(new Integer("3"));
   
    Object[] objArray = lhashSet.toArray();
   
    //display contents of Object array
    System.out.println("LinkedHashSet elements are copied into an Array. Now Array Contains..");
    for(int index=0; index < objArray.length ; index++)
      System.out.println(objArray[index]);
  }
}

Result


Related Tutorials