When all elements of a collection are of the same type, it is easier for you to work with an array
of that specific type.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String[] a) {
String elements[] = { "A", "B", "C", "D", "E" };
Set set = new HashSet(Arrays.asList(elements));
String[] strObj = new String[set.size()];
strObj = (String[]) set.toArray(strObj);
for (int i = 0; i < strObj.length; i++) {
System.out.println(strObj[i]);
}
System.out.println(set);
}
}
D
A
C
B
E
[D, A, C, B, E]