泛型

1.什么是泛型

JDK5中的泛型允许程序员在编写集合代码时,就限制集合的处理类型,从而把原来程序运行时可能发生问题,转变为编译时的问题,以此提高程序的可读性和稳定性(尤其在大型程序中更为突出)

2.使用泛型应该注意

  • 使用泛型时,泛型类型须为引用类型,不能是基本数据类型

 

  • 泛型是提供给javac编译器使用的,它用于限定集合的输入类型,让编译器在源代码级别上,即挡住向集合中插入非法数据。但编译器编译完带有泛型的java程序后,生成的class文件中将不再带有泛型信息,以此使程序运行效率不受到影响,这个过程称之为“擦除”
  • 为了兼任1.5以前的版本ArrayList<String> list = new ArrayList();和ArrayList list2 = new ArrayList<String>();这两种写法都是可以的
  • 以ArrayList<E>为例,<>念为typeof

3.一些例子

 1     @Test
 2     public void test1(){
 3         
 4         List<String> list = new ArrayList<>();
 5         list.add("1");
 6         list.add("2");
 7         list.add("3");
 8         list.add("4");
 9         list.add("5");
10         
11         Iterator<String> it = list.iterator();
12         while(it.hasNext()){
13             String str = it.next();
14             System.out.print(str+" ");
15         }
16         System.out.println();
17         for(String s : list){
18             System.out.print(s + " ");
19         }
20     }
 1     @Test
 2     public void test2(){
 3         
 4         Map<Integer, String> m = new LinkedHashMap<>();
 5         m.put(1, "one");
 6         m.put(2, "two");
 7         m.put(3, "three");
 8         
 9         //传统迭代  entryset
10         Set<Map.Entry<Integer, String>> set = m.entrySet();
11         Iterator<Map.Entry<Integer, String>> it = set.iterator();
12         while(it.hasNext()){
13             Map.Entry<Integer, String> me = it.next();
14             int key = me.getKey();
15             String value = me.getValue();
16             System.out.println(key + "=" + value);
17         }
18         System.out.println();
19         //用增强for来迭代
20         for(Map.Entry<Integer, String> entry : m.entrySet()){
21             int key = entry.getKey();
22             String value = entry.getValue();
23             System.out.println(key + "=" + value);
24         }
25     }

 4.自定义泛型

自定义泛型方法

 1 public class Demo1 {
 2     
 3     @Test
 4     public void test1(){
 5         System.out.println(b("hello") instanceof String);
 6         System.out.println(b(true) instanceof Boolean);
 7     }
 8     
 9     public <T> void a(T t){
10         System.out.println(t);
11     }
12     
13     public <T,E,K> void c(T t, E e, K k){
14         
15     }
16     
17     public <T> T b(T t){
18         return t;
19     }
20     
21 }

运行结果

true
true

自定义泛型方法的作用:调用泛型方法的时候再给泛型方法一个具体的类型。

如果类中很多方法用到同一种泛型,我们可以把泛型声明在类上(只作用在类的非静态成员方法上,静态成员方法泛型类型需要自己声明)

 1 public class Demo3<T> {
 2     
 3     
 4     public static void main(String[] args) {
 5         Demo3<String> d = new Demo3<String>();
 6         d.a("hello");
 7         System.out.println(d.b("java") instanceof String);
 8         System.out.println(Demo3.c(123) instanceof Integer);
 9     }
10     
11     public void a(T t){
12         System.out.println(t);
13     }
14     
15     public T b(T t){
16         return t;
17     }
18     
19     public static <T> T c(T t){
20         return t;
21     }
22     
23 }

运行结果

1 hello
2 true
3 true

 

posted @ 2019-04-26 13:29  Vamps  阅读(162)  评论(0编辑  收藏  举报