📄 treesettest.java
字号:
package connections;
import java.util.*;
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet<Person> ts = new TreeSet<Person>(new Person.PersonComparator());
ts.add(new Person(1,"one"));
ts.add(new Person(2,"two"));
ts.add(new Person(2,"three"));
ts.add(new Person(4,"four"));
Iterator<Person> it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Person {
private int num;
private String name;
public Person(int id, String name){
num = id;
this.name = name;
}
public String toString(){
return num + ": " + name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
static class PersonComparator implements Comparator<Person>{
public int compare(Person o1, Person o2){
int result;
result = (o1.getNum()> o2.getNum()) ? 1 : (o1.getNum() == o2.getNum() ? 0 : -1);
if (result == 0){
result = o1.getName().compareTo(o2.getName());
}
return result;
}
}
// public int compareTo(Person o){
// int result;
// result = (num> o.num) ? 1 : (num == o.num ? 0 : -1);
// if (result == 0){
// result = name.compareTo(o.name);
// }
// return result;
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -