Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 2
    Correct. LinkedHashSet contains elements in insertion order. Commented Dec 2, 2013 at 15:03
  • It's an interface, EVERYTHING depends on the implementation. List.get() could create a file containing the first 5 decimals of pi and throw a StackOverFlowException in some implementations, this does not imply that you can say "A list is something that can create files", since that is not part of the contract defined by the interface. The docs claim Set is modeled after the mathematical concept of a set, which by definition is un-ordered. Given a set in your code you cannot assume it is ordered without violating SOLID principles. Commented Nov 30, 2015 at 15:38
  • @kai, I usually keep LinkedHashSet on the left-hand side if the code relies on the ordering later on. I only use Set if I really use it like one, as you cannot assume that the underlying implementation is a LinkedHashSet or such, it may be today, but tomorrow the code changes and it will fail. Commented Nov 30, 2015 at 16:44
  • If you declare a LinkedHashSet you are not dealing with a Set, so making claims about how Sets should behave is hardly relevant. I'd say attributing (possible) orderedness to sets based on some implementations is akin to saying "Instances of Runnable have a run method meant to be run on some thread. Also they open a DB connection and read customer data depending on the implementation." Of course some implementations may do that, but that is not what is implied by the Runnable Interface. Commented Dec 1, 2015 at 8:28