I have a ArrayList which has some elements. The items in this list are repeated. I want to group the similar values and convert it into an array.
Currently my ArrayList is:-
ArrayList<String> isonC = new ArrayList<String>();
Value of isonC = `[a, a, a, a, b, c, c,C, c, c, c, d, d, d, d, d, d,d] I group the so:-
icons = isonC.toArray(new String[isonC.size()]);
icons = new HashSet<String>(Arrays.asList(icons)).toArray(new String[0]);
I get the values as:-
icons = [a, c, b,d]
Whereas the actual order of the elements should be:-
[a,b, c,d]
Why are the 2nd and 3rd values have interchanged?
Any suggesstions will be helpful. Thanks
HashSet,HashSetdoes not mantain order, the docs say "It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time" (docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)