title: In TreeSet, Sorting & Uniqueness of custom objects based on different properties
body: <p>Below is my Student class</p>
  
  <pre><code>class Student implements Comparable {
     String name;
     int rollNo;
  
     @Override
     public int compareTo(Object obj) {
          return ((Student)obj).name.compareTo(this.name);
     }
  } 
  </code></pre>
  
  <p>latest modification: but still no getting the right result</p>
  
  <pre><code>@Override
  public int compareTo(Object obj) {
      Student s = (Student) obj;
      if (name.equals(s.name)) { // achieving uniqueness
          return 0;
      } else {
          if (rollNo &lt; s.rollNo) {
              return -1;
          } else if (rollNo &gt; s.rollNo) {
              return 1;
          } else {
              // this makes `name` the second ordering option.
              // names don't equal here
              return name.compareTo(s.name);
          }
      }
  }
  </code></pre>
  
  <p>If I create object of TreeSet&lt;Student&gt;, I am getting sorted list of Student objects based on unique name &amp; ordered by name also.</p>
  
  <p>But I need unique student-name in my TreeSet&lt;Student&gt; with order by student-rollNo.</p>
  
  <p>Is it possible with Comparator? Can anybody help me, Every suggestion is appreciated.
  Thanks.</p>
  
  <p><b>UPDATE: here is the complete program:</b></p>
  
  <pre><code>public class Student implements Comparable {
  
      int rollNo;
      String name;
  
      Student(String n,int rno) {
          rollNo=rno;
          name=n;
      }
  
      /**
       * @param args
       */
      public static void main(String[] args) {
  
          TreeSet&lt;Student&gt; ts = new TreeSet&lt;Student&gt;();
          ts.add(new Student("bbb",2));
          ts.add(new Student("aaa",4));
          ts.add(new Student("bbb",2));
          ts.add(new Student("ccc",3));
          ts.add(new Student("aaa",1));
          ts.add(new Student("bbb",2));
          ts.add(new Student("bbb",5));
  
          System.out.println(ts);
  
      }
  
      @Override
      public int compareTo(Object obj) {
          Student s = (Student) obj;
          if (name.equals(s.name)) { // achieving uniqueness
              return 0;
          } else {
              if (rollNo &lt; s.rollNo) {
                  return -1;
              } else if (rollNo &gt; s.rollNo) {
                  return 1;
              } else {
                  // this makes `name` the second ordering option.
                  // names don't equal here
                  return name.compareTo(s.name);
              }
          }
      }
  
      @Override
      public String toString() {
          return name + rollNo;
      }
  }
  </code></pre>
  
  <p><b>Update:2: Thank you all for your suggestions, I still need some more :)</b></p>
  
  <pre><code>
  
  /*
   * Actual scenario is having different properties,
   * So here I am just relating my actual scenario with Student class
   */
  class Student implements Comparable {
      // sorting required on rollNo
      int rollNo;
      // Unique name is required
      String name;
  
      Student(String n, int rno) {
          rollNo = rno;
          name = n;
      }
  
      /**
       * 
       * @param args
       */
      public static void main(String[] args) {
  
          TreeSet&lt;Student&gt; tsName = new TreeSet&lt;Student&gt;();
          // here by default, order & uniqueness by name only
          tsName.add(new Student("ccc", 2));
          tsName.add(new Student("aaa", 4));
          tsName.add(new Student("ddd", 1));
          tsName.add(new Student("bbb", 3));
          tsName.add(new Student("ddd", 5));
          // output: aaa:4, bbb:3, ccc:2, ddd:1
          System.out.println(tsName);
  
          // creating new comparator for student RollNo
          TreeSet&lt;Student&gt; tsRollNo = new TreeSet&lt;Student&gt;(new Comparator&lt;Student&gt;() {
                      public int compare(Student stud1, Student stud2) {
                          return new Integer(stud1.rollNo).compareTo(stud2.rollNo);
                      }
                  });
          tsRollNo.addAll(tsName);
          System.out.println(tsRollNo);
          // now got the desire output: ddd:1, ccc:2, bbb:3, aaa:4
      }
  
      public boolean equals(Object obj) {
          // internally not used to check equality while adding objects
          // in TreeSet
          System.out.println("equals() for " + this + " & " + ((Student) obj));
          return false;// return false/true doesn't make any sense here
      }
  
      @Override
      public int compareTo(Object obj) {
          Student s = (Student) obj;
          // internally inside TreeSet, compareTo is used to decide
          // whether two objects are equal or not,
          // i.e. compareTo will return 0 for same object(here student name)
          System.out.println("compareTo() for " + this + " & " + ((Student) obj));
          // achieving uniqueness
          return name.compareTo(s.name);
      }
  
      @Override
      public String toString() {
          return name + ":" + rollNo;
      }
  }
  </code></pre>
  
  <p><strong>OUTPUT</strong>:</p>
  
  <pre><code>compareTo() for aaa:4 &amp; ccc:2
  compareTo() for ddd:1 &amp; ccc:2
  compareTo() for bbb:3 &amp; ccc:2
  compareTo() for bbb:3 &amp; aaa:4
  compareTo() for ddd:5 &amp; ccc:2
  compareTo() for ddd:5 &amp; ddd:1
  [aaa:4, bbb:3, ccc:2, ddd:1]
  [ddd:1, ccc:2, bbb:3, aaa:4]
  </code></pre>
  
  <p><B>Friends, whatever I got by using two Comparators, Is it possible to
  achieve the same while adding the objects ?? 
  I cannot first Add elements &amp; then use new comparator to achieve the desired order.
  <br>I am manipulating thousands of values so need to consider performance also.
  </B></p>