1

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

3
  • Would it suffice to re-order the items (in lexicographical order) after you remove the duplicate elements? Commented Jul 16, 2014 at 7:38
  • @JaeHeonLee not really.. I have the given array for the colors whose order need to be the way it is Commented Jul 16, 2014 at 8:05
  • You're using a HashSet, HashSet does 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) Commented Jul 16, 2014 at 8:28

2 Answers 2

5

Try using TreeSet instead of HashSet. Try something like below:-

TreeSet<String> mySet =  new TreeSet <String>(myArrayList);
myArray =  mySet.toArray(new String[mySet.size()]);
Sign up to request clarification or add additional context in comments.

Comments

1

From the documentation for LinkedHashSet:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

So the following should work, assuming that you want items which appear earlier in oldArray to appear earlier in newArray:

String[] newArray = new LinkedHashSet<String>(Arrays.asList(oldArray)).toArray(new String[0]);

2 Comments

I tried LinkedHashSet but somehow I get the same result except the last two items get swapped
@user3146095: if I try this with your example input (and the uppercase C changed to a lowercase c), I get the correct order. Are you sure that the ordering in the original ArrayList is correct? Maybe this is a case of "reinsertion" as mentioned in the documentation of the LinkedHashSet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.