Does the Java set keep order? The method returns Set for me and, presumably, the data is ordered, but iterating over Set, the data is unordered. Is there a better way to handle this? Do I need to change the method to return something other than Set?
The Set interface does not provide any order guarantees.
Set
Its subordinate interface, SortedSet is a collection that is sorted by some criterion. There are two standard containers in Java 6 that implement SortedSet . These are TreeSet and ConcurrentSkipListSet .
SortedSet
TreeSet
ConcurrentSkipListSet
In addition to the SortedSet interface, SortedSet is also the LinkedHashSet class. It remembers the order in which the elements were inserted into the set, and returns its elements in that order.
LinkedHashSet
LinkedHashSet is what you need.
Like many participants suggested using LinkedHashSet to preserve the order of the collection. U can wrap your set using this implementation.
SortedSet can be used for orderly ordering, but use LinkedHashSet for your purpose.
Also from the documents,
"This implementation eliminates the unspecified, chaotic order provided by the HashSet without increasing the cost associated with the TreeSet. It can be used to create a copy of the set that has the same order as the original, regardless of the original implementation:"
Source: http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html
A set is just an interface. To maintain order, you must use the specific implementation of this interface and the SortedSet sub-interface, such as TreeSet or LinkedHashSet. You can wrap your set like this:
Set myOrderedSet = new LinkedHashSet(mySet);
To maintain order, use a List or LinkedHashSet .
List
Here is a brief description of the order characteristics of the standard Set implementations available in Java:
For your specific case, you can sort the elements first and then use any of 1 or 2 (most likely LinkedHashSet or TreeSet ). Or, alternatively and more efficiently , you can simply add unsorted data to a TreeSet which will automatically take care of sorting.
LinkedHashSet is an ordered version of HashSet that supports a doubly linked list for all elements. Use this class instead of a HashSet if the iteration order is important to you.
From javadoc for Set.iterator() :
Set.iterator()
Returns an iterator over the elements of this set. Elements are returned in a specific order (if this set is not an instance of any class that provides a guarantee).
And, as shuuchan already said, TreeSet is a Set implementation that has a guaranteed order:
Elements are ordered using their natural ordering or using a comparator provided at the specified time of creation, depending on which constructor is used.
Typically, a set does not support ordering, such as a HashSet, to quickly find emelent, but you can try LinkedHashSet, it will store the order that you insert.
The Set interface itself does not specify any specific order. Still SortedSet .
The iterator returned by Set should not return data in order. See Two java.util.Iterators in the same set: do items need to be returned in the same order?
Only SortedSet can do Set ordering.