Java Set retain order?

JavaSortingSet

Java Problem Overview


Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the method need to be changed to return something other than a Set?

Java Solutions


Solution 1 - Java

The Set interface does not provide any ordering guarantees.

Its sub-interface SortedSet represents a set that is sorted according to some criterion. In Java 6, there are two standard containers that implement SortedSet. They are TreeSet and ConcurrentSkipListSet.

In addition to the SortedSet interface, there 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.

Solution 2 - Java

LinkedHashSet is what you need.

Solution 3 - Java

As many of the members suggested use LinkedHashSet to retain the order of the collection. U can wrap your set using this implementation.

SortedSet implementation can be used for sorted order but for your purpose use LinkedHashSet.

Also from the docs,

"This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashSet, without incurring the increased cost associated with TreeSet. It can be used to produce a copy of a set that has the same order as the original, regardless of the original set's implementation:"

Source : http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html

Solution 4 - Java

Set is just an interface. In order to retain order, you have to use a specific implementation of that interface and the sub-interface SortedSet, for example TreeSet or LinkedHashSet. You can wrap your Set this way:

Set myOrderedSet = new LinkedHashSet(mySet);

Solution 5 - Java

To retain the order use List or a LinkedHashSet.

Solution 6 - Java

Here is a quick summary of the order characteristics of the standard Set implementations available in Java:

  1. keep the insertion order: LinkedHashSet and CopyOnWriteArraySet (thread-safe)
  2. keep the items sorted within the set: TreeSet, EnumSet (specific to enums) and ConcurrentSkipListSet (thread-safe)
  3. does not keep the items in any specific order: HashSet (the one you tried)

For your specific case, you can either sort the items first and then use any of 1 or 2 (most likely LinkedHashSet or TreeSet). Or alternatively and more efficiently, you can just add unsorted data to a TreeSet which will take care of the sorting automatically for you.

Solution 7 - Java

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order.

Solution 8 - Java

From the javadoc for Set.iterator():

>Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

And, as already stated by shuuchan, a TreeSet is an implemention of Set that has a guaranteed order:

>The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Solution 9 - Java

Normally set does not keep the order, such as HashSet in order to quickly find a emelent, but you can try LinkedHashSet it will keep the order which you put in.

Solution 10 - Java

There are 2 different things.

  1. Sort the elements in a set. For which we have SortedSet and similar implementations.
  2. Maintain insertion order in a set. For which LinkedHashSet and CopyOnWriteArraySet (thread-safe) can be used.

Solution 11 - Java

The Set interface itself does not stipulate any particular order. The SortedSet does however.

Solution 12 - Java

Solution 13 - Java

Only SortedSet can do the ordering of the Set

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser840930View Question on Stackoverflow
Solution 1 - JavaNPEView Answer on Stackoverflow
Solution 2 - Javaxiaofeng.liView Answer on Stackoverflow
Solution 3 - JavaLakshmanView Answer on Stackoverflow
Solution 4 - JavajavatutorialView Answer on Stackoverflow
Solution 5 - JavaJHSView Answer on Stackoverflow
Solution 6 - JavaassyliasView Answer on Stackoverflow
Solution 7 - JavaDanail TsvetanovView Answer on Stackoverflow
Solution 8 - JavahmjdView Answer on Stackoverflow
Solution 9 - Javauser1335794View Answer on Stackoverflow
Solution 10 - JavaArunaView Answer on Stackoverflow
Solution 11 - JavaJens BorglandView Answer on Stackoverflow
Solution 12 - Javaejb_guyView Answer on Stackoverflow
Solution 13 - Javauser1417163View Answer on Stackoverflow