title: Storing multiple Address objects into a TreeSet from a Person class
body: <p>Basically, what I am trying to do is store multiple addresses for each Person object. I have decided to achieve this by storing Address objects in a TreeSet of the Person class. Essentially I would like to store the toString() of each Address object I create into my TreeSet. I pass the properties of an Address object through the addAddress() method of the Person class, which passes the properties to the constructor of the Address class.</p>
  
  <p>I receive an error on the specified line...</p>
  
  <pre><code>public class Person {
     private TreeSet&lt;Address&gt; addresses = new TreeSet&lt;Address&gt;();
  
     public void addAddress(String type, String street, String city, String state, String zip) {
       //ERROR ON THIS LINE BELOW
       Address addressObj = new Address(type, street, city, state, zip);
       addresses.add(addressObj);
     }
  }
  
  public class Address {
     private String type;
     private String street;
     private String city;
     private String state;
     private String zip;
  
     @Override
     public String toString() {
         return street + " " + city + " " + ", " + state + " " + zip + ": " + type;
     }
  
     public Address(String type, String street, String city, String state, String zip) {
         super();
         this.type = type;
         this.street = street;
         this.city = city;
         this.state = state;
         this.zip = zip;
     }
  }
  
  
  public class MainDriver {
  
     public static void main(String[] args) {
         Person p1 = new Person();
         p1.addAddress("Home", "321 Den Ave", "Orlando", "FL", "32792");
         p1.addAddress("Work", "4411 Alligator Court", "Orlando", "FL", "32792");
     }
  }
  </code></pre>
  
  <p>Here is the error message: </p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: Address cannot be cast to java.lang.Comparable
  at java.util.TreeMap.compare(TreeMap.java:1294)
  at java.util.TreeMap.put(TreeMap.java:538)
  at java.util.TreeSet.add(TreeSet.java:255)
  at Person.addAddress(Person.java:64)
  at MainDriver.main(MainDriver.java:9)
  </code></pre>
  
  <p>To be clear: I do not want to store the address in one string because I want to be able to manipulate each property individually later on. I also do not want the Address Class properties to be stored in the Person Class as I need multiple iterations of the Address Class to be able to store multiple addresses for each Person object.</p>