title: How to remove duplicates from ArrayList of type Object?
body: <p>I want to remove duplicates from <code>ArrayList</code> of type <code>Alerts</code> where <code>Alerts</code> is a class.</p>
  
  <p><strong>Class Alerts -</strong></p>
  
  <pre><code>public class Alerts implements Parcelable {
      String date = null;
      String alertType = null;
      String discription = null;
  
      public Alerts() {
  
      }
  
      public Alerts(String date, String alertType, String discription) {
          super();
          this.date = date;
          this.alertType = alertType;
          this.discription = discription;
      }
  }
  </code></pre>
  
  <p><strong>Here is how I added the elements -</strong></p>
  
  <pre><code>ArrayList&lt;Alerts&gt; alert = new ArrayList&lt;Alerts&gt;();
  Alerts obAlerts = new Alerts();
  
  obAlerts = new Alerts();
  obAlerts.date = Date1.toString();
  obAlerts.alertType = "Alert Type 1";
  obAlerts.discription = "Some Text";
  alert.add(obAlerts);
  
  obAlerts = new Alerts();
  obAlerts.date = Date2.toString();
  obAlerts.alertType = "Alert Type 1";
  obAlerts.discription = "Some Text";
  alert.add(obAlerts);
  </code></pre>
  
  <p><strong>What I want to remove from them-</strong></p>
  
  <p>I want all alerts which have unique <code>obAlerts.date</code> and <code>obAlerts.alertType</code>. In other words, remove duplicate <code>obAlerts.date</code> and <code>obAlerts.alertType</code> alerts.</p>
  
  <p><strong>I tried this -</strong>  </p>
  
  <pre><code>Alerts temp1, temp2;
  String macTemp1, macTemp2, macDate1, macDate2;
  
  for(int i=0;i&lt;alert.size();i++)
   {    
      temp1 = alert.get(i);  
      macTemp1=temp1.alertType.trim(); 
      macDate1 = temp1.date.trim();
  
      for(int j=i+1;j&lt;alert.size();j++)
      {
  
           temp2 = alert.get(j);  
          macTemp2=temp2.alertType.trim();  
          macDate2 = temp2.date.trim();
  
          if (macTemp2.equals(macTemp1) &amp;&amp; macDate1.equals(macDate2))  
          {   
             alert.remove(temp2);           
          } 
  
      }
   }
  </code></pre>
  
  <p><strong>I also tried-</strong></p>
  
  <pre><code>HashSet&lt;Alerts&gt; hs = new HashSet&lt;Alerts&gt;();
  hs.addAll(obAlerts);
  obAlerts.clear();
  obAlerts.addAll(hs);
  </code></pre>