Convert Set into array in Java

Description

The following code shows how to convert Set into array.

Example


    /*from w w  w . j  ava 2s.  c o  m*/

 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Set<Object> set = new HashSet<Object>();
    set.add("A");
    set.add(new Long(10));
    set.add(new Date());

    List<Object> list = new ArrayList<Object>(set);
    Object[] objects = list.toArray();

    for (int i = 0; i < objects.length; i++) {
      Object object = objects[i];
      System.out.println("Object = " + object);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet