java set属性_java – 在TreeSet中,基于不同属性的自定义对...

以下是我的学生班

class Student implements Comparable {

String name;

int rollNo;

@Override

public int compareTo(Object obj) {

return ((Student)obj).name.compareTo(this.name);

}

}

最新修改:但仍然没有得到正确的结果

@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);

}

}

}

如果我创建了TreeSet< Student>的对象,我将根据唯一名称&获取Student对象的排序列表.按名称排序.

但我需要在TreeSet中使用唯一的学生姓名< Student>按学生顺序排序.

比较器可以吗?任何人都可以帮助我,每个建议都表示赞赏.

谢谢.

更新:这是完整的程序:

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 ts = new TreeSet();

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 < 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);

}

}

}

@Override

public String toString() {

return name + rollNo;

}

}

更新:2:谢谢大家的建议,我还需要更多:)

/*

* 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 tsName = new TreeSet();

// 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 tsRollNo = new TreeSet(new Comparator() {

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;

}

}

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]

朋友,无论我使用两个比较器得到什么,是否有可能

添加对象时实现相同的??

我不能先添加元素&然后使用新的比较器来实现所需的顺序.

我正在操纵数以千计的价值,因此也需要考虑性能.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值