package com.itheima1;
import java.util.Iterator;
import java.util.TreeSet;
public class std implements Comparable {
private String name;
private int age;
std(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if (!(o instanceof std)) {
throw new RuntimeException("不是学生呢对象");
}
std t1 =(std)o;
if (this.age>t1.age) {
return 1;
}
if (this.age<t1.age) {
return -1;
}
return this.name.compareTo(t1.name);
}
public static void main(String[] args) {
TreeSet tree = new TreeSet<>();
tree.add(new std("zy", 18));
tree.add(new std("zy", 18));
tree.add(new std("zy1", 18));
tree.add(new std("zy", 20));
tree.add(new std("zy", 25));
Iterator it =tree.iterator();
tree.iterator();
while (it.hasNext()) {
std sd =(std)it.next();
System.out.println(sd.name+"__________"+sd.age);
}
}
}
|
|