What is the best way to create a List of "unique" objects in Java
<p>I am trying to create a unique list. I can not use a "Set" because I need to assign some of the values as I iterate though it.</p>

<p>Right now I doing this to create a unique list. Does anyone know a better way?</p>

<pre><code>List&lt;Thing&gt; things = previousThings.stream()
                       .collect(Collectors.toSet()).stream()
                       .collect(Collectors.toList());
</code></pre>

<p>I was thinking that if I convert it to a set and the back to a list it would eliminate any duplicate entries.  I think it needs to be a list so that I can use "List.set(..)" to update according to what might already be in my repository.</p>

<pre><code>for(int i = 0; i &lt; things.size(); i++) {
  if(things.get(i).id == null) {
    existingThing = thingRepository.getByName(things.get(i).getName());
    if(existingThing != null) {
      things.set(i, existingThing);
    } else {
      things.set(i, thingRepository.save(things.get(i));
    }
  }
}
</code></pre>