public class UpdateStu implements Comparable<Object>{ String name; long id; public UpdateStu(String name,long id) { // TODO Auto-generated constructor stub this.id = id; this.name = name; } public int compareTo(Object o) { UpdateStu upstu = (UpdateStu)o; int result = id > upstu.id? 1 : (id == upstu.id? 0 : -1); return result; } public String getString() { return name; } public void setString(String name) { this.name = name ; } public long getid() { return id; } public void setid(long id) { this.id = id; } public static void main(String[] args) { UpdateStu stu1 = new UpdateStu("李同学", 01011); UpdateStu stu2 = new UpdateStu("程同学", 01021); UpdateStu stu3 = new UpdateStu("王同学", 01051); UpdateStu stu4 = new UpdateStu("马同学", 01012); TreeSet<UpdateStu> tree = new TreeSet<>(); tree.add(stu1); tree.add(stu2); tree.add(stu3); tree.add(stu4); Iterator<UpdateStu> it = tree.iterator(); System.out.println("Set集合中的所有元素"); while(it.hasNext()){ UpdateStu stu = (UpdateStu)it.next(); System.out.println(stu.getid() + " " + stu.getString()); } it = tree.headSet(stu2).iterator(); System.out.println("截取前面部分的集合"); while(it.hasNext()){ UpdateStu stu = (UpdateStu)it.next(); System.out.println(stu.getid() + " " + stu.getString()); } it = tree.subSet(stu2, stu3).iterator(); System.out.println("截取中间部分的集合"); while(it.hasNext()){ UpdateStu stu = (UpdateStu)it.next(); System.out.println(stu.getid() + " " + stu.getString()); } } }