Below is my class of students
class Student implements Comparable { String name; int rollNo; @Override public int compareTo(Object obj) { return ((Student)obj).name.compareTo(this.name); } }
last modification: but still not getting the correct result
@Override public int compareTo(Object obj) { Student s = (Student) obj; if (name.equals(s.name)) { // achieving uniqueness return 0; } else { if (rollNo < s.rollNo) { return -1; } else if (rollNo > s.rollNo) { return 1; } else { // this makes `name` the second ordering option. // names don't equal here return name.compareTo(s.name); } } }
If I create a TreeSet <Student> object, I get a sorted list of Student objects based on a unique name and also sorted by name.
But I need a unique student name in my TreeSet <Student> with order student roll No..
Is this possible with a comparator? Can someone help me, every suggestion is welcome. Thank.
UPDATE: here is the complete program:
public class Student implements Comparable { int rollNo; String name; Student(String n,int rno) { rollNo=rno; name=n; } public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(); 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)) {
Update: 2: Thank you all for your suggestions, I still need more :)
/* * 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<Student> tsName = new TreeSet<Student>(); // 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<Student> tsRollNo = new TreeSet<Student>(new Comparator<Student>() { 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, // ie 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; } }
OUTPUT
compareTo() for aaa:4 & ccc:2 compareTo() for ddd:1 & ccc:2 compareTo() for bbb:3 & ccc:2 compareTo() for bbb:3 & aaa:4 compareTo() for ddd:5 & ccc:2 compareTo() for ddd:5 & ddd:1 [aaa:4, bbb:3, ccc:2, ddd:1] [ddd:1, ccc:2, bbb:3, aaa:4]
Friends, no matter what I get using two comparators, is it possible to achieve the same when adding objects? I cannot add the elements first and then use the new comparator to achieve the desired order.
I manipulate thousands of values, so productivity must be considered.