Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There isn't a way you can check for it -- but you can ensure it anyway, by simply copying the set into a collection that does have that property. A <code>LinkedHashSet</code> would do the trick, but if all you need is the iteration, an <code>ArrayList</code> would probably serve you better.</p> <pre><code>List&lt;Foo&gt; parameters = new ArrayList&lt;&gt;(parametersSet); </code></pre> <p>Now <code>parameters</code> will always return an iterator with the same ordering.</p> <p>That said, you'd probably be fine with Evgeniy Dorofeev's suggestion, which points out that even the sets that don't guarantee a particular ordering usually <em>do</em> have a stable ordering (even if they don't guarantee it). <code>HashSet</code> acts that way, for instance. You'd actually have to have a pretty funky set, or take active randomization measures, to not have a stable ordering.</p> <p><code>HashSet</code>'s ordering is not guaranteed, but it depends on the hash codes of its elements as well as the order in which they were inserted; they don't want to guarantee anything because they don't want to lock themselves into any one strategy, and even this loose of a contract would make for essentially random order if the objects' hash codes came from <code>Object.hashCode()</code>. Rather than specifying an ordering with complex implications, and then saying it's subject to change, they just said there's no guarantees. But those are the two factors for ordering, and if the set isn't being modified, then those two factors are going to be stable from one iteration to the next.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload