集合的泛型

一:泛型的普通使用

1.集合泛型的好处

  是一种安全机制,将运行时可能出现的问题转移到编译时期

  避免了强转 

2.程序示例

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 
 4 public class Test49 {
 5 
 6     public static void main(String[] args) {
 7         ArrayList<String> a=new ArrayList<>();
 8         a.add("java1");
 9         a.add("java2ww");
10         a.add("java3");
11         Iterator<String> it=a.iterator();
12         while(it.hasNext()){
13             String s=it.next();
14             System.out.println(s.length());
15         }
16     }
17 
18 }

3.简单使用

  Comparator比较器同样有泛型,这时,compare就会自带和比较器相同的类型

 1 import java.util.Comparator;
 2 import java.util.TreeSet;
 3 
 4 public class Test50 {
 5 
 6     public static void main(String[] args) {
 7         TreeSet<String> t=new TreeSet<>(new wor());
 8         t.add("aaaaa");
 9         t.add("bbb");
10         t.add("abcd");
11         t.add("bbd");
12         System.out.println(t);
13     }
14 
15 }
16 
17 class wor implements Comparator<String>{
18 
19     @Override
20     public int compare(String o1, String o2) {
21         int num=new Integer(o1.length()).compareTo(new Integer(o2.length()));
22         if(num==0){
23             return o1.compareTo(o2);
24         }
25         return num;
26     }
27     
28 }

4.泛型类

  当引用类型不确定的时候,可以使用泛型类。当类上确定了一个类型之后,再操作就是这个类型的操作,除非重新定义一个对象,就是换成新的类型

 1 public class Test51 {
 2 
 3     public static void main(String[] args) {        
 4         out<String> out1=new out<String>();
 5         out1.show("aa");
 6         out1.print("jui");
 7     }
 8 }
 9 class out<T>{
10     public void show(T t){
11         System.out.println("show :"+t);
12     }
13     public void print(T t){
14         System.out.println("print :"+t);
15     }
16 }

5.泛型方法

  不像泛型类,当类已经确定之后,方法的类型必须跟着类的类型运行。

  相同的方法可以被任意的类型使用。

 1 public class Test52 {
 2 
 3     public static void main(String[] args) {
 4         out1 out=new out1();
 5         out.show("hhh");
 6         out.show(7);
 7         out.print("uuuu");
 8     }
 9 
10 }
11 class out1{
12     public <T> void show(T t){
13         System.out.println("show :"+t);
14     }
15     public <Q> void print(Q q){
16         System.out.println("print :"+q);
17     }
18 }

6.泛型类和泛型方法的结合

 1 public class Test53 {
 2 
 3     public static void main(String[] args) {
 4         out2<String> out=new out2<>();
 5         out.show("hhh");               //这个只能跟着泛型类的方法走
 6         out.print(5);
 7     }
 8 
 9 }
10 class out2<T>{
11     public void show(T t){             //这个是属于泛型类的方法
12         System.out.println("show :"+t);
13     }
14     public <Q> void print(Q q){        //这个属于泛型方法
15         System.out.println("print :"+q);
16     }
17 }

7.静态方法的泛型问题

  静态方法不可以访问泛型类上定义的类型,因为是静态,但是可以定义在方法上,就是静态方法泛型。

 1 public class Test54 {
 2 
 3     public static void main(String[] args) {
 4         out3<String> out=new out3<>();
 5         out.show("aa");
 6         out.print(88);
 7         out.method("cccccc");
 8     }
 9 
10 }
11 class out3<T>{
12     public void show(T t){             //这个是属于泛型类的方法
13         System.out.println("show :"+t);
14     }
15     public <Q> void print(Q q){        //这个属于泛型方法
16         System.out.println("print :"+q);
17     }
18     public static <W> void method(W w){//静态方法的泛型
19         System.out.println("method :"+w);
20     } 
21 }

8.泛型接口

 1 interface inter<T>{
 2     void show(T t);
 3 }
 4 class impInter1 implements inter<String>{   //当继承接口时,类型已经知道
 5     @Override
 6     public  void show(String t) {
 7         System.out.println("t :"+t);
 8     }    
 9 }
10 class impInter2<T> implements inter<T>{   //当继承接口时,类型仍然不知道,在类后面同样跟着T
11     @Override
12     public  void show(T t) {
13         System.out.println("t :"+t);
14     }    
15 }
16 public class Test55 {
17     public static void main(String[] args) {
18         impInter1 imp0=new impInter1();            //类型已经被接口给定义好了
19         imp0.show("yubh");
20         impInter2<String> imp1=new impInter2<>();
21         imp1.show("yubh");
22         impInter2<Integer> imp2=new impInter2<>();
23         imp2.show(888);
24     }
25 
26 }

二:泛型的高级使用,泛型的限定

1.通配符?

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 
 4 public class Test56 {
 5 
 6     public static void main(String[] args) {
 7         ArrayList<String> al = new ArrayList<String>();
 8         al.add("abc1");
 9         al.add("abc2");
10         al.add("abc3");
11         ArrayList<Integer> al1 = new ArrayList<Integer>();
12         al1.add(4);
13         al1.add(7);
14         al1.add(1);
15         printColl(al);
16         printColl(al1);
17     }
18     public static void printColl(ArrayList<?> al)    //通配符
19     {
20         Iterator<?> it = al.iterator();
21         while(it.hasNext())
22         {
23             System.out.println(it.next());
24         }
25     }
26 }

2.? extends E,上限定

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 
 4 class Persony
 5 {
 6     private String name;
 7     Persony(String name)
 8     {
 9         this.name = name;
10     }
11     public String getName()
12     {
13         return name;
14     }
15 }
16 class Studenty extends Persony
17 {
18     Studenty(String name)
19     {
20         super(name);
21     }
22 
23 }
24 public class Test57 {
25     public static void main(String[] args) {        
26         ArrayList<Persony> al = new ArrayList<Persony>();
27         al.add(new Persony("abc1"));
28         al.add(new Persony("abc2"));
29         al.add(new Persony("abc3"));
30         printColl(al);
31         ArrayList<Studenty> al1 = new ArrayList<Studenty>();
32         al1.add(new Studenty("abc--1"));
33         al1.add(new Studenty("abc--2"));
34         al1.add(new Studenty("abc--3"));
35         printColl(al1);
36     }
37     public static void printColl(ArrayList<? extends Persony> al)   //? extends Persony 是上线
38     {
39         Iterator<? extends Persony> it = al.iterator();
40         while(it.hasNext())
41         {
42             System.out.println(it.next().getName());
43         }
44     }
45 }

3.下限

 1 import java.util.Collection;
 2 import java.util.Comparator;
 3 import java.util.Iterator;
 4 import java.util.TreeSet;
 5 
 6 public class Test58 {
 7     public static void main(String[] args) {
 8         TreeSet<Studentt> ts = new TreeSet<Studentt>(new Comp());
 9         ts.add(new Studentt("abc1"));
10         ts.add(new Studentt("abc5"));
11         ts.add(new Studentt("abc3"));
12         printColl(ts);
13     }
14     public static void printColl(TreeSet<Studentt> ts)
15     {
16         Iterator<Studentt> it = ts.iterator();
17         while(it.hasNext())
18         {
19             System.out.println(it.next().getName());
20         }
21     }
22 }
23 class Persont
24 {
25     private String name;
26     Persont(String name)
27     {
28         this.name = name;
29     }
30     public String getName()
31     {
32         return name;
33     }
34 }
35 
36 class Studentt extends Persont
37 {
38     Studentt(String name)
39     {
40         super(name);
41     }
42 
43 }
44 class Comp implements Comparator<Persont>            //studentt是下限,Comparable<? super E>是用法
45 {
46     public int compare(Persont s1,Persont s2)
47     {
48         return s1.getName().compareTo(s2.getName());
49     }
50 }
原文地址:https://www.cnblogs.com/juncaoit/p/6200765.html