title: Ordering Set by Instant
body: <p>I have a Java <code>Set</code> that I give information to:</p>
  
  <pre><code>Set&lt;myData&gt; dataLocations = getData(location);
  </code></pre>
  
  <p>I would like to sort this Set I have tried a sortedSet and couldn't get it to work, so I tried this</p>
  
  <pre><code>dataLocations = dataLocations.stream().sorted(Comparator.comparing(myData -&gt; myData.expDtTm)).collect(Collectors.toSet());
  </code></pre>
  
  <p>The only problem is that in the Java documentation it doesn't guarantee retaining any orders. So I tried this:</p>
  
  <pre><code>TreeSet&lt;myData&gt; sortedDataLocations = dataLocations.stream().sorted(Comparator.comparing(myData -&gt; myData.expDtTm)).collect(Collectors.toCollection(TreeSet&lt;myData&gt;));
  </code></pre>
  
  <p>needless to say it didn't work so anyone that has any other ideas they would be much appreciated.</p>