Generic泛型

1.问题

未使用泛型时,元素的类型不安全;操作麻烦,可能需要强制转换

复制代码
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import org.junit.Test;
 4 import day8.Customer;
 5 
 6 public class GenericTest {
 7     @Test
 8     public void testCollectionGeneric() {
 9         List l1=new ArrayList();
10         l1.add(new Customer(1,"AA"));
11         l1.add(new Customer(3,"CC"));
12         l1.add(new Customer(2,"BB"));
13               //类型不安全:可以放入任何类型的元素
14         l1.add(10);
15              //操作麻烦:从集合中取元素时,需要进行类型的强制转换
16         Customer c0=(Customer) l1.get(0);
17         System.out.println(c0);
18 
19         }
20}
复制代码

2.引入

复制代码
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import org.junit.Test;
 4 
 5 public class GenericTest {
 6     @Test
 7     public void testCollectionGeneric() {
 8 
 9                 //声明一个Customer类型的List
10         List<Customer> l2=new ArrayList<Customer>();
11         l2.add(new Customer(1,"AA"));
12         l2.add(new Customer(3,"CC"));
13         l2.add(new Customer(2,"BB"));        
14 //      l2.add(10);      无法放入其他类型的元素
15         Customer c1=l2.get(1);            //获取元素无需强转
16         System.out.println(c1);
17 
18         }
19 }
复制代码

3.场合

<E,K...>:泛型参数,可以在使用其他类型的场合都可以使用泛型类型;泛型参数可以有多个。

1.声明泛型类

复制代码
 1 public class GenericClass<E,K> {
 2     private E i;               //变量类型由泛型类决定
 3 
 4     private K j;
 5     public E getI() {       
 6         return i;
 7     }
 8     public void setI(E i) {      //方法的类型和参数也由泛型类决定
 9         this.i = i;
10     }
11 }
12 
13 public class GenericTest1 {
14     public static void main(String args[]){
15         GenericClass<Integer,String> gc=new GenericClass<Integer,String>();       //确定泛型类
16         gc.setI(123);           //参数为int类型
17         System.out.println(gc.getI());
18     }
19 }
复制代码

2.声明泛型方法

泛型的声明,必须在public,static,final,abstract等之后,返回值声明之前;可以拥有多个泛型参数。

复制代码
 1 public class GenericMethod{
 2     public  <E> E getI(E i){
 3         return i;        
 4     }    
 5     public <E> E getN(int i){
 6         return null;
 7     }    
 8     public <E> void display(E i) {
 9         System.out.println(e.getClass());
10     }    
11 }
12 public class GenericTest1 {
13     public static void main(String args[]){        
14         GenericMethod gm=new GenericMethod();
15         gm.getI(1);
16         gm.getN(1);
17         System.out.println(gm.getI(1));
18         System.out.println(gm.getN(1));
19         gm.display(1);
20         gm.display("1");
21         gm.display(12.13);
22     }
23 }
复制代码

3.声明泛型集合

确保了元素的类型安全,操作简单

复制代码
import java.util.HashMap;
import java.util.Map;

public class GenericTest {
    @Test
    public void testCollectionGeneric() {

         Map<String,Integer> score=new HashMap<String,Integer>();
        score.put("1", 90);
        score.put("2", 80);
        score.put("3", 70);
//        score.replace("1", 90, 100);       
        /*Set<String> keyset=score.keySet();
        for(String key : keyset){
            Integer value=score.get(key);
            System.out.println(key+" : "+value);
        }*/
        for(Map.Entry<String, Integer> entry:score.entrySet()){
            String key=entry.getKey();
            int value=entry.getValue();
            int hashcode=entry.hashCode();                                    
            System.out.println(hashcode+" -> "+key+" : "+value);
        }

}

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值