Java set keep order?

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?

+152
java sorting set order
May 25 '12 at 10:27
source share
12 answers

The Set interface does not provide any order guarantees.

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 .

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.

+219
May 25 '12 at 10:30
source share

LinkedHashSet is what you need.

+87
May 25 '12 at 10:29
source share

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

+15
Dec 07 '12 at 7:30
source share

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); 
+9
May 25 '12 at 10:29
source share

To maintain order, use a List or LinkedHashSet .

+7
May 25 '12 at 10:31
source share

Here is a brief description of the order characteristics of the standard Set implementations available in Java:

  1. save insertion order: LinkedHashSet and CopyOnWriteArraySet (turning-safe)
  2. save elements sorted in a set: TreeSet , EnumSet (specific for enumerations) and ConcurrentSkipListSet (thread-oriented)
  3. doesn't store items in any specific order: HashSet (the one you tried)

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.

+6
May 25 '12 at 10:36
source share

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.

+4
Apr 04 '18 at 11:58
source share

From javadoc for 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.

+3
May 25 '12 at 10:30
source share

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.

+3
May 25 '12 at 10:30
source share

The Set interface itself does not specify any specific order. Still SortedSet .

0
May 25 '12 at 10:30
source share

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?

0
May 25 '12 at 10:31
source share

Only SortedSet can do Set ordering.

-2
May 25 '12 at 10:33
source share



All Articles