泛型
jdk1.5出现的安全机制
好处:
1.将运行时期出现的问题ClassCastException转移到编译时期。
2.避免了强制转换的麻烦。
<>:什么时候用?当操作的引用数据类型不确定的时候。就使用<>。将要操作的引用数据类型传入即可。
其实<>就是一个用于接收具体引用数据类型的参数范围。
在程序中,只要用到了带有<>的类或者接口,就要明确传入的具体引用数据类型。
泛型技术是给编译器使用的技术,确保了类型的安全。
泛型的擦除:运行时,会将泛型去掉,生成的class文件中是不带泛型的,称为泛型的擦除。
为什么会擦除呢?因为兼容运行的类加载器。
泛型的补偿:在运行时,通过获取元素的类型进行转换动作。不用使用者在强制转换了。
在集合中的应用
package com.monfolld.p2.bean;
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person(){
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int compareTo(Person obj){
Person p=(Person)obj;
int temp=this.age-p.age;
return temp==0?this.name.compareTo(p.name):temp;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
package com.monfolld.p3.comparator;
import com.monfolld.p2.bean.Person;
import java.util.Comparator;
public class ComparatorByName 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;
}
}
import com.monfolld.p3.comparator.ComparatorByName;
import com.monfolld.p2.bean.Person;
import java.util.Iterator;
import java.util.TreeSet;
public class GenericDemo_2 {
public static void main(String[] args){
TreeSet<Person> ts=new TreeSet<Person>(new ComparatorByName());
ts.add(new Person("lisi4",21));
ts.add(new Person("lisi3",23));
ts.add(new Person("lisi",24));
ts.add(new Person("lisi0",01));
Iterator<Person> it=ts.iterator();
while (it.hasNext()){
Person p=it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}
}
泛型类
//在jdk1.5后,使用泛型来接收类中要操作的引用数据类型
//泛型类,什么时候用?当类中的操作的引用数据类型不确定的时候,就使用泛型来表示
public class Tool<QQ>{
private QQ q;
public QQ getQ() {
return q;
}
public void setQ(QQ q) {
this.q = q;
}
}
package com.monfolld.generic.demo;
import com.monfolld.p2.bean.Student;
import com.monfolld.p4.generic.Tool;
public class GenericDemo_3 {
public static void main(String[] args){
Tool<Student> tool=new Tool<Student>();
tool.setQ(new Student());
Student stu=(Student)tool.getQ();
}
}
泛型方法
public class Tool<QQ>{
private QQ q;
public QQ getQ() {
return q;
}
public void setQ(QQ q) {
this.q = q;
}
//将泛型定义在方法上
public<W> void show(W str){
System.out.println("show"+str.toString());
}
public void print(String str){
System.out.println("printf"+str);
}
//当方法静态时,不能访问类上定义的泛型,如果静态方法使用泛型,只能将泛型定义在方法上
public static <Y> void method(Y obj){
System.out.println("method"+obj);
}
}
package com.monfolld.generic.demo;
import com.monfolld.p4.generic.Tool;
public class GenericDemo_4 {
public static void main(String[] args){
Tool<String> tool=new Tool<String>();
tool.show(new Integer(4));
tool.show("abc");
tool.print("haha");
tool.method("haha");
tool.method(new Integer(9));
}
}
泛型接口
package com.monfolld.generic.demo;
public class GenericDemo_5 {
public static void main(String[] args){
InterImpl in=new InterImpl();
in.show("abc");
InterImpl2<Integer> in2=new InterImpl2<Integer>();
in2.show(4);
}
}
interface Inter<T>{
public void show(T t);
}
class InterImpl implements Inter<String>{
public void show(String str){
System.out.println("show"+str);
}
}
class InterImpl2<Q> implements Inter<Q>{
public void show(Q q){
System.out.println("show"+q);
}
}
泛型限定
泛型的通配符:?//未知类型
在不明确类型并不对类型进行操作用?
package com.monfolld.generic.demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericAdvanceDemo {
public static void main(String[] args){
ArrayList<String> al=new ArrayList<String>();
al.add("abc");
al.add("haha");
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.add(19);
al2.add(21);
printCollection(al);
printCollection(al2);
}
/*迭代并打印集合中的元素*/
public static void printCollection(Collection<?> al){
Iterator<?> it=al.iterator();
while (it.hasNext()){
System.out.println(it.next().toString());
}
}
}
上限:? extends E:接收E类型或者E的子类对象。当创建一个容器,规定不能任何东西都打印,只能打印Person的子类
package com.monfolld.generic.demo;
import com.monfolld.p2.bean.Person;
import com.monfolld.p2.bean.Student;
import com.monfolld.p2.bean.Worker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericAdvanceDemo_2 {
public static void main(String[] args){
ArrayList<Worker> al=new ArrayList<Worker>();
al.add(new Worker("abc",30));
al.add(new Worker("abc4",34));
ArrayList<Student> al2=new ArrayList<Student>();
al2.add(new Student("stu1",11));
al2.add(new Student("stu2",22));
printCollection(al);
printCollection(al2);
}
/*迭代并打印集合中的元素*/
/*如果用Person左右泛型不一样
public static void printCollection(Collection<Person> al){就相当于//Collection<Person> al=new ArrayList<Student>();*/
public static void printCollection(Collection<? extends Person> al){//只接收Person和Person的子类
Iterator<? extends Person> it=al.iterator();
while (it.hasNext()){
Person p=it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}
}
一般在存储元素的时候都使用上限,因为这样取出都是按照上限类型来运算的,不会出现安全隐患。
addAll(Collection<? extends E> c) //按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾
下限:? super E:接收E类型或者E的子类对象。
package com.monfolld.generic.demo;
import com.monfolld.p2.bean.Person;
import com.monfolld.p2.bean.Student;
import com.monfolld.p2.bean.Worker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericAdvanceDemo_2 {
public static void main(String[] args){
ArrayList<Student> al2=new ArrayList<Student>();
al2.add(new Student("stu1",11));
al2.add(new Student("stu2",22));
printCollection(al2);
}
public static void printCollection(Collection<? super Student> al){
Iterator<? super Student> it=al.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
通常,对集合中的元素进行取出操作时,可以是下限
package com.monfolld.generic.demo;
import com.monfolld.p2.bean.Person;
import com.monfolld.p2.bean.Student;
import com.monfolld.p2.bean.Worker;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.Iterator;
public class GenericDemo_4{
public static void main(String[] args){
TreeSet<Person> al1=new TreeSet<Person>(new CompByName());
al1.add(new Person("abc1",30));
al1.add(new Person("abc4",34));
al1.add(new Person("abc3",33));
TreeSet<Worker> al2=new TreeSet<Worker>(new CompByName());//Person可以接收Worker进行比较
al2.add(new Worker("work1",30));
al2.add(new Worker("work2",34));
TreeSet<Student> al3=new TreeSet<Student>(new CompByName());//Person可以接收Student进行比较
al3.add(new Student("stu1",11));
al3.add(new Student("stu2",22));
//迭代器能接收Worker类型或者Worker的父类
Iterator<? super Worker> it=al1.iterator();//Iterator<? super Worker> it=al2.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
/*class TreeSet<E>
{
Tree(Comparator<? super E> comp);
}
什么时候用下限呢?通常对集合中的元素进行取出操作时,可以使用下限。
*/
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;
}
}
通配符的体现
boolean containsAll(Collection<?> c) //如果此列表包含指定 集合的所有元素,则返回true
package com.monfolld.generic.demo;
import com.monfolld.p2.bean.Person;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericAdvenceDemo_5 {
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<Person> al2=new ArrayList<Person>();
al1.add(new Person("abc222",30));
al1.add(new Person("abc422222",34));
ArrayList<String> al4=new ArrayList<String>();
al4.add("abcdef");
al4.add("abc");
al1.containsAll(al2);
}
public static void printCollection(Collection<?> al1){
Iterator<?> it=al1.iterator();
while (it.hasNext()){
System.out.println(it.next().toString());
}
}
}
class MyCollection<E>{
public boolean containsAll(Collection<?> coll){ //传入未知类型尽心比较 ?
return true;
}
}