标签:style blog color java os io 数据 2014
package stu.love.v; import java.util.*; //泛型限定上限的应用 class Demo12 { public static void main(String[] args) { Collection<Student> c = new ArrayList<Student>(); c.add(new Student("zhaosi",23)); c.add(new Student("lisi",25)); c.add(new Student("wangwu",20)); //TreeSet(Collection<? extends E> c) // class TreeSet<E> //{ //} TreeSet<Person> ts = new TreeSet<Person>(c); Iterator<Person> ite = ts.iterator(); while(ite.hasNext()) { sop(ite.next()); } } public static void sop(Object obj) { System.out.println(obj); } }
package stu.love.v; import java.util.*; import stu.love.v.Demo11; /*// 子类 特殊的 比较器 Student class ComByAge implements Comparator<Student> { public int compare(Student s1,Student s2) { int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); if(num==0) return s1.getName().compareTo(s2.getName()); return num; } } // 子类 特殊的 比较器 Teacher class ComByAge2 implements Comparator<Teacher> { public int compare(Teacher s1,Teacher s2) { int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); if(num==0) return s1.getName().compareTo(s2.getName()); return num; } }*/ //父类的比较器 <Person> class CompareByAge implements Comparator<Person> { public int compare(Person s1,Person s2) { int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); if(num==0) return s1.getName().compareTo(s2.getName()); return num; } } class Demo13 { //TreeSet<E>(Comparator<? super E> comparator) 定义比较器时,可以是E类型,还可以是E的父类型,E在创建集合对象时确定 public static void main(String[] args) { TreeSet<Student> t1 = new TreeSet<Student>(new CompareByAge()); t1.add(new Student("zhaosi",23)); t1.add(new Student("lisi",25)); t1.add(new Student("wangwu",20)); TreeSet<Teacher> t2 = new TreeSet<Teacher>(new CompareByAge()); t2.add(new Teacher("wang",38)); t2.add(new Teacher("lilaoshi",48)); t2.add(new Teacher("zhanglaoshi",58)); } }
标签:style blog color java os io 数据 2014
原文地址:http://blog.csdn.net/love_javc_you/article/details/38306341