JAVA 集合(3) 泛型

摘要:JAVA 集合(3) 泛型

泛型
發生異常現象,使用泛型解決,JDK1.5出現的,用於解決安全問題
public class Main{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add("ajij");
        al.add("ceccsd");
        al.add("wcegess");
        al.add(4); //發生異常
       
        Iterator it = al.iterator();
        while(it.hasNext()){
            String s = (String)it.next();
            sop(s);
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}
 
泛型好處
將運行時期出現問題classCastException,轉移到編譯時期,方便解決問題,讓運行時期問題減少,提升安全性,同時避免了強制轉型的麻煩
 
修改上例
public class Main{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add("ajij");
        al.add("ceccsd");
        al.add("wcegess");
       
        Iterator it = al.iterator();
        while(it.hasNext()){
            String s = it.next();
            sop(s);
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

泛型格式,通過<>來定義要操作的引用數據類型,通常在集合框架中很常見,只要看到<>都要,<>用來接收類型,當使用集合時,將集合中要存儲的數據類型作為參數傳遞到<>中即可
 
比較字串長度,長度一樣再比較字串大小
public class Main{
    public static void main(String[] args){
        TreeSet ts = new TreeSet(new LenComparator());
        ts.add("ajij");
        ts.add("ceccsw");
        ts.add("ajia");
        ts.add("ceccsd");
        ts.add("wcegess");
       
        Iterator it = ts.iterator();
        while(it.hasNext()){
            String s = it.next();
            sop(s);
            // ajia, ajij, ceccsd, ceccsw, wcegess
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}
class LenComparator implements Comparator{
    public int compare(String o1, String o2){
        int num = new Integer(o1.length()).compareTo(new Integer(o2.length()));
        if(num == 0)
            return o1.compareTo(o2);
        return num;
    }
}

 

泛型前作法

class Worker {

}
class Student{
   
}
class Tool{
    private  Object obj;
    public void setObject(Object obj){
        this.obj = obj;
    }
    public Object getObject(){
        return obj;
    }
}

public class Main{
    public static void main(String[] args){
        Tool t = new Tool();
        t.setObject(new Worker());
        Worker w = (Worker)t.getObject();
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

使用泛型
當類別中要操作的引用數據類型不確定時,早期定義Object來擴展,現在則定義泛型來完成擴展
class Tool<E>{
    private  E obj;
    public void setObject(E obj){
        this.obj = obj;
    }
    public E getObject(){
        return obj;
    }
}
public class Main{
    public static void main(String[] args){
        Tool<Worker> u = new Tool<Worker>();
        u.setObject(new Worker());
        Worker worker = u.getObject();
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

泛型類別定義,在整個類別有效,如果被方法使用,那泛型類別的物件要明確操作具體類型後,所有操作的類型就固定了,為了讓不同方法可以操作不同類型,而且類型不確定,那可以將泛型定義在方法上

class Demo{
    public void show(T t){
        System.out.println("show: " + t);
    }
    public void print(T t){
        System.out.println("print: " + t);
    }
}

public class Main{
    public static void main(String[] args){
        Demo d = new Demo();
        d.show(new Integer(4));
        d.print(9);
    }
}

 

改用方法定義泛型,更具彈性

class Demo{
    public  void show(T t){
        System.out.println("show: " + t);
    }
    public  void print(Q q){
        System.out.println("print: " + q);
    }
}

public class Main{
    public static void main(String[] args){
        Demo d = new Demo();
        d.show(new Integer(4));
        d.print("123");
    }
}

 

靜態方法不可以訪問類別中定義的泛型,如果靜態方法操作的應用數據類型不確定,可以將泛型定義在方法上

class Demo{
    public static  void method(W t){
        System.out.println("method: " + t);
    }
}
public class Main{
    public static void main(String[] args){
        Demo d = new Demo();
        Demo.method("Test");
    }
}

 

泛型定義在介面上
interface Inter{
    void show(T t);
}
class InterImpl implements Inter{
    public void show(T t){
        System.out.println("Show: " + t);
    }
}
public class Main{
    public static void main(String[] args){
        InterImpl i = new InterImpl();
        i.show(new Integer(111));
    }
}

 

<?>: 通配符,也可以理解為佔位符
泛型的限定
? extends E: 可以接收E類型或E的子類型,上限
? super E: 可以接收E類型或E的父類型,下限

public class Main{
    public static void main(String[] args){
        ArrayList a1 = new ArrayList();
        a1.add("abc");
        a1.add("abc1");
        a1.add("abc2");
       
        ArrayList a2 = new ArrayList();
        a2.add(1);
        a2.add(2);
        a2.add(3);
       
        print(a1);
        print(a2);
    }
    public static void print(ArrayList arrayList){
        Iterator it = arrayList.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

 

 

public class Main{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        al.add(new Person("a"));
        al.add(new Person("b"));
        al.add(new Person("c"));
        printColl(al);
       
        ArrayList ala = new ArrayList();
        ala.add(new Student("ddd"));
        ala.add(new Student("b2"));
        ala.add(new Student("c3"));
        printColl(al);
    }
   
    //泛型的限定
    public static void printColl(ArrayList al){
        Iterator it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next().getName());
        }
    }
}

class Person{
    private String name;
    Person(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

class Student extends Person{
    Student(String name){
        super(name);
    }
}

 

使用TreeSet來運用泛型擴展

public class Main{
    public static void main(String[] args){
        TreeSet ts = new TreeSet(new Comp());
        ts.add(new Student("stuent--1"));
        ts.add(new Student("stuent--5"));
        ts.add(new Student("stuent--3"));
        ts.add(new Student("stuent--2"));
        ts.add(new Student("stuent--4"));

        TreeSet ts1 = new TreeSet(new Comp());
        ts1.add(new Worker("work---5"));
        ts1.add(new Worker("work---3"));
        ts1.add(new Worker("work---1"));
        ts1.add(new Worker("work---2"));
        ts1.add(new Worker("work---4"));       
       
        Iterator it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next().getName());
        }
        Iterator it1 = ts1.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next().getName());
        }
    }
}
class Comp implements Comparator{
    public int compare(Person p1, Person p2){
        return p2.getName().compareTo(p1.getName());
    }
}
class Person{
    private String name;
    Person(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

class Student extends Person{
    Student(String name){
        super(name);
    }
}

class Worker extends Person{
    Worker(String name){
        super(name);
    }
}