談談JAVA中泛型?的作用


在java泛型中,?表示任何類型,比如Set<?>表示Set中支持任何類型元素。那這跟直接用Set有什么區別呢?我們先看以下兩段代碼:

使用Set:

1 public static void printSet(Set s) {
2     s.add("2");
3     for (Object o : s) {
4         System.out.println(o);
5     }
6 }

使用Set<?>:

1 public static void printSet(Set<?> s) {
2     s.add(10);//this line is illegal 
3     for (Object o : s) {
4         System.out.println(o);
5     }
6 }

很遺憾,第二段代碼的第2行出錯了。原因是:我們不知道Set中的具體類型,所以我們不能把任何類型的元素添加到Set中(null除外)。這樣做是為了保證這個Set中的元素類型一致。而使用不帶泛型的Set不存在這樣的限制,你可以把不同類型的元素添加進去,當然,這樣做是不值得推薦的。

 

不過以下代碼對於Set<?>是合法的:

public static void main(String[] args) {
    HashSet<Integer> s1 = new HashSet<Integer>(Arrays.asList(1,2,3));
    HashSet<Integer> s2 = new HashSet<Integer>(Arrays.asList(4,2,3));
 
    System.out.println(getUnion(s1, s2));
}
 
public static int getUnion(Set<?> s1, Set<?> s2){
    int count = s1.size();
    for(Object o : s2){
        if(!s1.contains(o)){
            count++;
        }
    }
    return count;
}

泛型?的作用是:當你需要使用泛型,但是不知道或者不關心它的具體類型的時候,你可以使用它。

 

另外,由於java泛型的缺陷(參考java編程思想),以下代碼是不合法的:

1     public static void main(String... args){
2         Set<Integer> s = new HashSet<Integer>(Arrays.asList(1,2,3));
3         print(s);//this line is illegal
4     }
5 
6     public static void print(Set<Number> s){
7 
8     }

但是這樣寫是合法的:

1     public static void main(String... args){
2         Set<Integer> s = new HashSet<Integer>(Arrays.asList(1,2,3));
3         print(s);//OK
4     }
5 
6     public static void print(Set<? extends Number> s){
7 
8     }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM