package ejbwebdemo; import java.util.TreeSet; public class course { public static void main(String[] args) { TreeSet<student> list = new TreeSet<student>(); list.add(new student("rrr", 1221)); list.add(new student("qqq", 1)); list.add(new student("www", 121)); System.out.println(list.toString());//对象序列化后,调用toString()输出 } } class student implements Comparable<student> { String name = null; int id = 0; public student(String name, int id) { this.id = id; this.name = name; } public int compareTo(student o) { return this.name.compareTo(o.name); } // 只需要指定铵什么比较,具体比较方法内部自己实现 // 对象序列化输出//对象序列化后,调用toString()输出
/* * Object public String toString() { return getClass().getName() + "@" + * Integer.toHexString(hashCode()); } */@Overridepublic String toString() {return "student [name=" + name + ", id=" + id + "]";}}原文:http://blog.csdn.net/yangdong3129/article/details/22422137