title: Why does my TreeSet not add anything beyond the first element?
body: <p>I have several arrays in the form:</p>
  
  <pre><code>private static String[] patientNames = { "John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr" };
  </code></pre>
  
  <p>Then I make a TreeSet like this:</p>
  
  <pre><code>TreeSet&lt;Patient&gt; patTreeSet = new TreeSet&lt;Patient&gt;();
  </code></pre>
  
  <p>Where Patient is a different class that makes "Patient" objects.
  Then I loop through each element in my arrays to create several patients and add them to my <code>patTreeSet</code> like this:</p>
  
  <pre><code>for(int i = 0; i&lt; patientNames.length; i++){
       Date dob = date.getDate("MM/dd/yyyy", patientBirthDates[i]);
       Patient p = new PatientImpl(patientNames[i], patientSSN[i], dob);
  
       patTreeSet.add(p);
  }
  </code></pre>
  
  <p>But when I go to check my <code>patTreeSet.size()</code> it only returns "1" - why is this?</p>
  
  <p>I know my objects are working well because when I try to do the same thing but with <code>ArrayList</code> instead, everything works fine. So I'm guessing I'm using the TreeSet wrong.</p>
  
  <p>If it helps, Patient has a method called getFirstName(), and when I try to do the following:</p>
  
  <pre><code>Iterator&lt;Patient&gt; patItr = patTreeSet.iterator();
  
  while(patItr.hasNext()){
      System.out.println(patItr.next().getFirstName());
  
  }
  </code></pre>
  
  <p>Then only "John" prints, which obviously shouldn't be the case... So, am I totally misusing the TreeSet?</p>
  
  <p>Thanks in advance for any help!</p>
  
  <p><strong>EDIT</strong> below</p>
  
  <p>================PatientImpl Class====================</p>
  
  <pre><code>public class PatientImpl implements Patient, Comparable{
  
      Calendar cal = new GregorianCalendar();
      private String firstName;
      private String lastName;
      private String SSN;
      private Date dob;
      private int age;
      private int thisID;             
      public static int ID = 0;       
  
  
  
      public PatientImpl(String fullName, String SSN, Date dob){
  
          String[] name = fullName.split(" ");
          firstName = name[0];
          lastName = name[1];
  
          this.SSN = SSN;
  
          this.dob = dob;
  
          thisID = ID += 1;
      }
  
  @Override
      public boolean equals(Object p) {
  
          //for some reason casting here and reassigning the value of p doesn't take care of the need to cast in the if statement...
          p = (PatientImpl) p;
  
          Boolean equal = false;
          //make sure p is a patient before we even compare anything
          if (p instanceof Patient) {
  
              Patient temp = (Patient) p;
  
              if (this.firstName.equalsIgnoreCase(temp.getFirstName())) {
                  if (this.lastName.equalsIgnoreCase(temp.getLastName())) {
                      if (this.SSN.equalsIgnoreCase(temp.getSSN())) {
                          if(this.dob.toString().equalsIgnoreCase(((PatientImpl) p).getDOB().toString())){
                              if(this.getID() == temp.getID()){
                                  equal = true;
                              }
                          }
                      }
                  }
              }
           }
          return equal;
      }
  </code></pre>
  
  <p>and then all the getters are below, as well as the compareTo() method from the Comparable interface</p>