See Example:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class Example1 {
public static void main(String args[]) {
// HashSet declaration
HashSet<String> hashset = getHashSet();
}
public static HashSet<String> getHashSet(){
// Adding elements to the HashSet
HashSet<String> hashset = new HashSet<String>();
hashset.add("Item4");
hashset.add("Item2");
hashset.add("Item6");
hashset.add("Item1");
hashset.add("Item3");
hashset.add("Item5");
System.out.println(" Values of Hashset = "+hashset);
// Creating an Array (convert hashset to Array
String[] array = new String[hashset.size()];
hashset.toArray(array);
System.out.println(" Result convert hashset to Array: ");
for(String temp : array){
System.out.println(temp);
}
return hashset;
}
}
//result:
Values of Hashset = [Item1, Item2, Item5, Item6, Item3, Item4]
Result convert hashset to Array:
Item1
Item2
Item5
Item6
Item3
Item4
EmoticonEmoticon