I would like to use unsorted generic collection to store values.
Set<Integer> map = new HashSet<Integer>();
map.Add( new Integer( 3 ) );
map.Add( new Integer( 2 ) );
map.Add( new Integer( 4 ) );
map.Add( new Integer( 1 ) );
I suppose the elements would be 3,2,4,1.
Then I would like to create an array from this set:
Integer[] arr = ( Integer[] )map.toArray( new Integer[map.size()] );
And I’m surprised because the elements in arr are in different order than I put into map.
The deal is to get an array like this:
arr[0] = 3;
arr[1] = 2;
arr[2] = 4;
arr[3] = 1;
What should I do for this?
HashSetdoes not guarantee that the order will remain same. If you want to maintain order then useArrayList.