package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class GenericAdvanceDemo { /** * @param args */ public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("abc"); al.add("hehe"); ArrayList<Integer> al2 = new ArrayList<Integer>(); al2.add(5); al2.add(67); printCollection(al); printCollection(al2); } /** * 迭代并打印集合中元素。 * @param al */ //!!!接受任意类型参数的集合方法写法,通配符,printCollection(Collection<?> al) public static void printCollection(Collection<?> al) { Iterator<?> it = al.iterator(); while(it.hasNext()){ // T str = it.next(); // System.out.println(str); System.out.println(it.next().toString()); } } }
package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo2 { /** * @param args */ public static void main(String[] args) { ArrayList<Person> al = new ArrayList<Person>(); al.add(new Person("abc",30)); al.add(new Person("abc4",34)); ArrayList<Student> al2 = new ArrayList<Student>(); al2.add(new Student("stu1",11)); al2.add(new Student("stu2",22)); ArrayList<String> al3 = new ArrayList<String>(); al3.add("stu3331"); al3.add("stu33332"); printCollection(al2); printCollection(al); } /** * 迭代并打印集合中元素。 * * 可以对类型进行限定: * ? extends E:接收E类型或者E的子类型对象。上限(上面封顶,从封顶处无限往下)!,用的多 * * ? super E :接收E类型或者E的父类型。下限(下面封顶,从封顶处无限往上)! * @param al */ //!!!!!!!只接受继承某父类的子类,其他的都不接受,这样做的好处可以对接受类型进行限定 //如果写extends Object就可以接受所有的类型 /*public static void printCollection(Collection<? extends Person> al) {//Collection<Dog> al = new ArrayList<Dog>() Iterator<? extends Person> it = al.iterator(); while(it.hasNext()){ // T str = it.next(); // System.out.println(str); // System.out.println(it.next().toString()); Person p = it.next(); System.out.println(p.getName()+":"+p.getAge()); } }*/ //!!!!只能接受自己类型和父类 public static void printCollection(Collection<? super Student> al){ Iterator<? super Student> it = al.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo3 { /** * @param args */ public static void main(String[] args) { ArrayList<Person> al1 = new ArrayList<Person>(); al1.add(new Person("abc",30)); al1.add(new Person("abc4",34)); ArrayList<Student> al2 = new ArrayList<Student>(); al2.add(new Student("stu1",11)); al2.add(new Student("stu2",22)); ArrayList<Worker> al3 = new ArrayList<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); ArrayList<String> al4 = new ArrayList<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 al1.addAll(al2); al1.addAll(al3); System.out.println(al1.size()); // printCollection(al2); // printCollection(al); } } /* * 一般在存储元素的时候都是用上限,因为这样取出都是按照上限类型来运算的。不会出现类型安全隐患。 * */ class MyCollection<E>{ public void add(E e){ } //!!!能够接受E及其子类 public void addAll(MyCollection<? extends E> e){ } }
package cn.itcast.p5.generic.advance.demo; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo4 { /** * @param args */ public static void main(String[] args) { TreeSet<Person> al1 = new TreeSet<Person>(new CompByName()); al1.add(new Person("abc4",34)); al1.add(new Person("abc1",30)); al1.add(new Person("abc2",38)); //!!!!!默认构造方法中,Tree(Comparator<? super Student> comp);因此,可以用与Person一致的方法接受 TreeSet<Student> al2 = new TreeSet<Student>(new CompByName()); al2.add(new Student("stu1",11)); al2.add(new Student("stu7",20)); al2.add(new Student("stu2",22)); TreeSet<Worker> al3 = new TreeSet<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); TreeSet<String> al4 = new TreeSet<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 // al1.addAll(al2); // al1.addAll(al3); // System.out.println(al1.size()); Iterator<Student> it = al2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } /* * class TreeSet<Worker> * { * Tree(Comparator<? super Worker> comp); * } * !!!重点看这个TreeSet中有这个构造函数用来比较,因此具备了Comparator直接使用父类接受一切子类的情况 * 什么时候用下限呢?通常对集合中的元素进行取出操作时,可以是用下限。 * */ class CompByName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByStuName implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByWorkerName implements Comparator<Worker>{ @Override public int compare(Worker o1, Worker o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } }
package cn.itcast.p5.generic.advance.demo; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo4 { /** * @param args */ public static void main(String[] args) { TreeSet<Person> al1 = new TreeSet<Person>(new CompByName()); al1.add(new Person("abc4",34)); al1.add(new Person("abc1",30)); al1.add(new Person("abc2",38)); //!!!!!默认构造方法中,Tree(Comparator<? super Student> comp);因此,可以用与Person一致的方法接受 TreeSet<Student> al2 = new TreeSet<Student>(new CompByName()); al2.add(new Student("stu1",11)); al2.add(new Student("stu7",20)); al2.add(new Student("stu2",22)); TreeSet<Worker> al3 = new TreeSet<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); TreeSet<String> al4 = new TreeSet<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 // al1.addAll(al2); // al1.addAll(al3); // System.out.println(al1.size()); Iterator<Student> it = al2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } /* * class TreeSet<Worker> * { * Tree(Comparator<? super Worker> comp); * } * !!!重点看这个TreeSet中有这个构造函数用来比较,因此具备了Comparator直接使用父类接受一切子类的情况 * 什么时候用下限呢?通常对集合中的元素进行取出操作时,可以是用下限。 * */ class CompByName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByStuName implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByWorkerName implements Comparator<Worker>{ @Override public int compare(Worker o1, Worker o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } }