2

Edit: this is actually for an Android application, which means (as far as I know), I can only use a HashSet.

Set<String> set = new HashSet<String>();

set.add("1");
set.add("2");
set.add("3");
set.add("4");
set.add("5");
set.add("6");
set.add("7");
set.add("8");

String[] array = set.toArray(new String[0]); // convert the set to an array

System.out.println(Arrays.toString(array)); // test what the set looks like

The output of that is [3, 2, 1, 7, 6, 5, 4, 8]

I expected [1, 2, 3, 4, 5, 6, 7, 8] because I assumed it would add the Strings to the Set in order of when I added them.

In my application, it's essential that the order of the Set is in the order that the elements were added to the Set.

Is there any reason that this Set is out of order? Or is there a way to put it back into the order that the elements were added in?

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • Take look at [LinkedHashSet](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html) – AurA Dec 23 '13 at 08:27

6 Answers6

8

Hlo.

  1. HashSet - doesn't maintains insertion order.
  2. LinkedHashSet - Maintains insertion order.
  3. TreeSet - Keeping data in natural order or you can define custom order and pass the comparator.

You can go with second option.

Set<String> set = new LinkedHashSet<String>();
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
Karthikeyan Sukkoor
  • 968
  • 2
  • 6
  • 24
7

Use a LinkedHashSet if you want to order the elements of the set. For more details, see this link.

aquaraga
  • 4,138
  • 23
  • 29
  • I'm not sure that I can because this is actually for a SharedPreference in Android. – Michael Yaworski Dec 23 '13 at 08:26
  • @mikeyaworski I don't think that matters. You could take a look at http://developer.android.com/reference/java/util/Set.html. It clearly specifies LinkedHashSet as one of the sub-classes. – aquaraga Dec 23 '13 at 08:31
  • Android can use a LinkedHashSet, but SharedPreferences can't store a LinkedHashSet. However, you did answer the question. This wasn't supposed to be about Android. – Michael Yaworski Dec 23 '13 at 08:45
3

The data structure HashSet is unordered and unsorted collection. If you want your elements to be sorted using natural order, you can use TreeSet If you want to maintain using insertion order, use LinkedHashSet

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2

No it's by design. A HashSet will insert your strings in buckets appropriate for your data, in a way that minimises the retrieval speed.

If you want to preserve lexographic order then use a TreeSet. (With the example you give that would work but only because your input data are sorted).

If you want to preserve insertion order then use a LinkedHashSet.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Yes there is a reason and you would have known if you read the first lines of it's javadoc:

This class implements the Set interface, backed by a hash table (actually a HashMap instance). 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.

NickDK
  • 5,159
  • 2
  • 18
  • 11
1

TheHashSet doesn't maintain a predictable order, it will depend on the hashCode of the object reference. If you would like to maintain the order in which the elements are inserted, use a LinkedHashSet. If you want to maintain the elements always sorted, use a TreeSet.

1218985
  • 7,531
  • 2
  • 25
  • 31