Java Day 18

泛型
 提高了安全性,由运行时期出现的问题转移到编译时期
 应用场景
  当操作的引用数据类型不确定的时候,就是用<>。
  其实<>就是一个用于接收具体引用数据类型的参数范围。
 
擦除与补偿
 泛型技术是给编译器使用的技术,用于编译时期。确保了类型的安全。
 运行时,会将泛型去掉,生成的class文件中是不带泛型的,这个称为泛型的擦除。
 兼容运行的类加载器。
 补偿:在运行时,获取元素的类型进行转换,无须强制类型转换。

 1 package com.company.Day018;
 2 
 3 
 4 /**
 5  * Created by junius on 2016/10/8.
 6  */
 7 class Tool<T>{
 8     private T t;
 9 
10     public T getT() {
11         return t;
12     }
13 
14     public void setT(T t) {
15         this.t = t;
16     }
17 
18     public void show(T str){ //泛型方法
19         System.out.println("show:"+str);//str.length()支持么?
20     }
21 
22     public <w> void show1(w str){
23         System.out.println("show1:"+str);
24     }
25 
26     public static <ww> void show2(ww str){
27         System.out.println("show2:"+str);
28     }
29     /*
30     public static  void show2(T str){
31         System.out.println("show2:"+str);
32     }
33     */
34 
35     public void print(String str){
36         System.out.println("print:"+str);
37     }
38 }
39 /*public class Tool{
40     private Person person;
41 
42     public Person getPerson() {
43         return person;
44     }
45 
46     public void setPerson(Person person) {
47         this.person = person;
48     }
49 }*/
50 public class GenericClassDemo {
51     public static void main(String[] args){
52 
53     }
54 }

泛型应用
  TreeSet:
 
自定义泛型类
 
泛型方法
 public <w> void show(w str){ }//不是所有的字符串方法都支持
 
 如果是静态方法,不能访问 类 上定义的泛型。
 如果静态方法要使用泛型,只能将泛型定义在方法上。

 public static <w> void show(w obj){ } 

泛型接口

 1 package com.company.Day018;
 2 
 3 /**
 4  * Created by junius on 2016/10/8.
 5  */
 6 
 7 interface Inter<T>{
 8     public void show(T t);
 9 }
10 
11 class InterImpl2<Q> implements Inter<Q>{
12     public void show(Q  q){
13         System.out.println("show:"+q);
14     }
15 }
16 
17 class InterImpl implements Inter<String>{
18     public void show(String str){
19         System.out.println("show :"+str);
20     }
21 }
22 
23 public class GenericInterfaceDemo {
24     public static void main(String[] args){
25         InterImpl in = new InterImpl();
26         in.show("abc");
27         InterImpl2<Integer> in2 = new InterImpl2<Integer>();
28         in2.show(5);
29     }
30 }

泛型限定-上限
 通配符:? 未知类型
 ? extends E 接收 E 类型或子类型对象

 1 package com.company.Day018;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * Created by junius on 2016/10/9.
 7  */
 8 public class GenericAdvanceDemo {
 9     public static void main(String[] args){
10         ArrayList<String> al = new ArrayList<String>();
11         al.add("abc");
12         al.add("aaa");
13 
14         List<Integer> al2 = new ArrayList<Integer>();
15         al2.add(22);
16         al2.add(33);
17 
18         printCollection(al);
19         printCollection(al2);
20     }
21 
22     public static void printCollection(Collection<?> al) {
23         Iterator<?> it = al.iterator();
24 
25         while(it.hasNext()){
26 //            ? str = it.next();
27             System.out.println(it.next());
28         }
29     }
30 }
 1 package com.company.Day018;
 2 
 3 import com.company.Java_Bean.Person;
 4 import com.company.Java_Bean.Student;
 5 import com.company.Java_Bean.Worker;
 6 
 7 import java.util.ArrayList;
 8 import java.util.Collection;
 9 import java.util.Iterator;
10 import java.util.List;
11 
12 /**
13  * Created by junius on 2016/10/9.
14  */
15 public class GenericAdvanceDemo2 {
16     public static void main(String[] args){
17         ArrayList<Worker> al = new ArrayList<Worker>();
18         al.add(new Worker("zhangsan",23));
19         al.add(new Worker("lisi",24));
20 
21         ArrayList<Student> al2 = new ArrayList<Student>();
22         al2.add(new Student("zhangsan",13));
23         al2.add(new Student("lisi2",14));
24 
25         printCollection(al);
26         printCollection(al2);
27     }
28 
29     public static void printCollection(Collection<? extends Person> al) {
30         Iterator<? extends Person> it = al.iterator();
31 
32         while(it.hasNext()){
33             Person p = it.next();
34             System.out.println(p.getName());
35         }
36     }
37 }

泛型限定-下限
 ? super E :接收 E 或 父类型

上限的体现
 存元素

下限的体现
 

集合技巧
 set
 TreeSet
 HashSet LinkedHashSet
 List
 
Map集合
 value put(key value)
 
 void clear()
 value remove(key)

 value get(key)

keySet
 
entrySet
 返回May.Entry类型
 
方法value
 Collection values()

常见子类
 Hashtable 哈希表 同步 null不允许作为键、值
   Properties 配置文件--> I/O 技术
 HashMap null 允许作为键、值
 TreeMap

 1 package com.company.Day018.Map;
 2 
 3 import java.util.*;
 4 import java.util.zip.Inflater;
 5 
 6 /**
 7  * Created by junius on 2016/10/9.
 8  */
 9 public class MapDemo {
10     public static void main(String[] args){
11         Map<Integer,String> map = new HashMap<Integer,String>();
12         //method(map);
13         method_2(map);
14     }
15     public static void method_2(Map<Integer,String> map){
16         map.put(8,"wanli1");
17         map.put(9,"wanli2");
18         map.put(12,"wanli23");
19         map.put(18,"wanli3");
20 
21         Set<Integer> keyset = map.keySet();
22 
23         Iterator<Integer> it = keyset.iterator();
24         while(it.hasNext()){
25             Integer key = it.next();
26             System.out.println(key+":"+map.get(key));
27         }
28 
29         Set<Map.Entry<Integer,String>> entrySet = map.entrySet();
30         Iterator<Map.Entry<Integer,String>> it2 = entrySet.iterator();
31 
32         while(it2.hasNext()){
33             Map.Entry<Integer,String> me = it2.next();
34             Integer key = me.getKey();
35             String value = me.getValue();
36             System.out.println(key+":-:"+value);
37 
38 
39         }
40 
41     }
42     public static void method(Map<Integer,String> map){
43         System.out.println(map.put(1,"lisi"));
44         System.out.println(map.put(11,"lisi2"));
45         System.out.println(map.put(11,"lisi3"));
46         System.out.println(map.put(13,"lisi4"));
47         System.out.println(map);
48 
49         System.out.println(map.remove(11));
50         System.out.println(map);
51 
52 
53     }
54 }
原文地址:https://www.cnblogs.com/zhuzhuqwa/p/5947528.html