title: Make a unique list of objects Java
body: <p>I have an ArrayList filled with objects with attributes name and time. I would like to remove duplicates based on the name and keep only records with the latest time. So I have overriden <code>equals</code> and <code>hashcode</code> for name in my object and used code like this.</p>
  
  <pre><code>private List&lt;ChangedRecentlyTO&gt; groupRecords(List&lt;ChangedRecentlyTO&gt; toList) {
      changedRecentlyList.clear(); //static list
      for(ChangedRecentlyTO to : toList) {
          if(!changedRecentlyList.contains(to)) {
              changedRecentlyList.add(to);
          } else {
              if(changedRecentlyList.get(changedRecentlyList.lastIndexOf(to)).getTimeChanged().before(to.getTimeChanged())) {
                  changedRecentlyList.remove(to);
                  changedRecentlyList.add(to);
              }
          }
      }
      return changedRecentlyList;
  }
  </code></pre>
  
  <p>But I am wondering, is there a better solution?I was thinking about using Set but I am not able to figure out how should I put there the time criterion. </p>