JAVA泛型实例解析

JAVA泛型大体分为:基本泛型和通配符泛型,在大多数项目下一般使用基本泛型的概率会大一些,此文主要就是对最近学习的泛型进行的一个总结,不足的望给予批评与指导!下例子

   案例1:

 

Java代码 复制代码  收藏代码
  1. public class GenericTest {   
  2.        
  3.     /*在JAVA1.4中的集合应用是无泛型的,我们可以向内部传入各种种类的值*/  
  4.     public void Generic14(){   
  5.         ArrayList co = new ArrayList();   
  6.         co.add(1);   
  7.         co.add(1L);   
  8.         co.add("aaa");   
  9.         /*但是我们需要在获取的时候对其进行强制类型转换*/  
  10.         int i = (Integer)co.get(0);   
  11.         System.out.println(i);   
  12.     }   
  13.        
  14.     /*JDK1.5中添加了泛型关键字为<>(如<String>,其中放置的基本数据类型)这样有效的减少强制转换*/  
  15.     public void Generic15() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{   
  16.         ArrayList<Integer> cos = new ArrayList<Integer>();   
  17.         cos.add(1);   
  18.         cos.add(2);   
  19.         /*因为经过泛型所以cos.add("abc");不能被编译器所通过,但是泛型的样式在编译过后将被剔除仍为ArrayList形式*/  
  20.         /*以下为反射的例子,去做验证,利用反射的方法调用ArrayList的add方法向cos对象中插入字符串abc*/  
  21.         Method  method = ArrayList.class.getMethod("add", Object.class);   
  22.         //           对象   参数值   
  23.         method.invoke(cos, "abc");   
  24.         //遍历cos对象   
  25.         for(Object c : cos){   
  26.             System.out.println(c);   
  27.         }   
  28.     }   
  29.        
  30.     /*例子2*/  
  31.     public void GenericlA(){   
  32.         List<Integer> A = new ArrayList<Integer>();   
  33.         List<String> B = new ArrayList<String>();   
  34.         //泛型在编译时已经被去除并不带入到运行过程   
  35.         System.out.println(A.getClass()==B.getClass());   
  36.     }   
  37.        
  38.     public void Connection(){   
  39.         //Collection父类   
  40.         Collection c = new ArrayList();   
  41.         //Vector类型相当于Arraylist内部进行了syn的处理,效率会相对Array的类型要底   
  42.         Vector<String> d = new Vector();   
  43.     }   
  44.        
  45.     public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {   
  46.         GenericTest genericTest = new GenericTest();   
  47. //      genericTest.Generic14();   
  48. //      genericTest.Generic15();   
  49. //      genericTest.GenericlA();   
  50.         genericTest.GAll();   
  51.     }   
  52.        
  53.     public static void GAll(){   
  54.         //map的迭代   
  55.         HashMap<String, Integer> hashMaps = new HashMap<String, Integer>();   
  56.         hashMaps.put("dhy"11);   
  57.         hashMaps.put("slb",5);   
  58.         //但是map型不能直接进行迭代必须转化为set类型才能   
  59.         Set<Map.Entry<String, Integer>> entrySet = hashMaps.entrySet();   
  60.         for (Entry<String, Integer> entry : entrySet) {   
  61.             System.out.println(entry.getKey()+" : "+entry.getValue());   
  62.         }   
  63.     }   
  64.        
  65. }  
public class GenericTest {
	
	/*在JAVA1.4中的集合应用是无泛型的,我们可以向内部传入各种种类的值*/
	public void Generic14(){
		ArrayList co = new ArrayList();
		co.add(1);
		co.add(1L);
		co.add("aaa");
		/*但是我们需要在获取的时候对其进行强制类型转换*/
		int i = (Integer)co.get(0);
		System.out.println(i);
	}
	
	/*JDK1.5中添加了泛型关键字为<>(如<String>,其中放置的基本数据类型)这样有效的减少强制转换*/
	public void Generic15() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		ArrayList<Integer> cos = new ArrayList<Integer>();
		cos.add(1);
		cos.add(2);
		/*因为经过泛型所以cos.add("abc");不能被编译器所通过,但是泛型的样式在编译过后将被剔除仍为ArrayList形式*/
		/*以下为反射的例子,去做验证,利用反射的方法调用ArrayList的add方法向cos对象中插入字符串abc*/
		Method  method = ArrayList.class.getMethod("add", Object.class);
		//           对象   参数值
		method.invoke(cos, "abc");
		//遍历cos对象
		for(Object c : cos){
			System.out.println(c);
		}
	}
	
	/*例子2*/
	public void GenericlA(){
		List<Integer> A = new ArrayList<Integer>();
		List<String> B = new ArrayList<String>();
		//泛型在编译时已经被去除并不带入到运行过程
		System.out.println(A.getClass()==B.getClass());
	}
	
	public void Connection(){
		//Collection父类
		Collection c = new ArrayList();
		//Vector类型相当于Arraylist内部进行了syn的处理,效率会相对Array的类型要底
		Vector<String> d = new Vector();
	}
	
	public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		GenericTest genericTest = new GenericTest();
//		genericTest.Generic14();
//		genericTest.Generic15();
//		genericTest.GenericlA();
		genericTest.GAll();
	}
	
	public static void GAll(){
		//map的迭代
		HashMap<String, Integer> hashMaps = new HashMap<String, Integer>();
		hashMaps.put("dhy", 11);
		hashMaps.put("slb",5);
		//但是map型不能直接进行迭代必须转化为set类型才能
		Set<Map.Entry<String, Integer>> entrySet = hashMaps.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry.getKey()+" : "+entry.getValue());
		}
	}
	
}

    案例2:

Java代码 复制代码  收藏代码
  1. public class GenericWTest {   
  2.        
  3.     public static void printCollection (Collection<Object> collection){   
  4.         for(Object c : collection){   
  5.             System.out.println(c);   
  6.         }   
  7.     }   
  8.        
  9.     public static void main(String[] args) {   
  10.         ArrayList<String> test = new ArrayList<String>();   
  11.         Collection<String> cn = new ArrayList<String>();   
  12.         /* 本身定义的是Collection<Object>  
  13.          * 传入的是ArrayList(Collection的子类型)<String(Object的子类型)>  
  14.          * 但是却说类型不对,原因在于Collecttion<Object>这样定义后就是要传入<object>的值  
  15.          * 所以在泛型中没有父类与子类之分,所以要用下面的方法  
  16.          */  
  17.         ArrayList<Object> test2 = new ArrayList<Object>();   
  18.         test.add("a");   
  19.         test.add("b");   
  20.         printCollection(test2);   
  21.            
  22.         String[] strs = new String[]{"aaa","bbb","ccc"};   
  23.         ArrayList lists = new ArrayList();   
  24.         lists = (ArrayList) copy1(strs, lists);   
  25.         for (Object list : lists) {   
  26.             System.out.println(list);   
  27.         }   
  28.   
  29. //      泛型的追溯特性,String和Date的都是Serializable的子类   
  30.         copy(new String[10], new Date[10]);   
  31.            
  32. //      泛型的传播特性,其中为Date类型()   
  33. //      copy1(new String[10],new ArrayList<Date>());   
  34.     }   
  35.   
  36.     /*为此推出了?这个通配符*/  
  37.     public static void printCollection2 (Collection<?> collection){   
  38.         /*但是引入这个通配符后,不能对这样的Collection对象去调用与参数有关的方法*/  
  39.         /*collection.add("aa");这样的语句是报错的,因为他不能确定泛型是什么类型的*/  
  40.         for(Object c : collection){   
  41.             System.out.println(c);   
  42.         }   
  43.     }   
  44.     //13:servlet jsp jmail jdbc jndi xml    
  45.     public static void ClassTest(){   
  46.         Class<?> a = null;   
  47.     }   
  48.     // 泛型方法    
  49.     public static <T> T[] copy(T[] forms,T[] to){   
  50.         return null;   
  51.     }   
  52.        
  53.     public static <T> Collection<T> copy1(T[] froms,Collection<T> to){   
  54.         for(T yuan : froms){   
  55.             to.add(yuan);   
  56.         }   
  57.         return to;   
  58.     }   
  59.        
  60. }  
public class GenericWTest {
	
	public static void printCollection (Collection<Object> collection){
		for(Object c : collection){
			System.out.println(c);
		}
	}
	
	public static void main(String[] args) {
		ArrayList<String> test = new ArrayList<String>();
		Collection<String> cn = new ArrayList<String>();
		/* 本身定义的是Collection<Object>
		 * 传入的是ArrayList(Collection的子类型)<String(Object的子类型)>
		 * 但是却说类型不对,原因在于Collecttion<Object>这样定义后就是要传入<object>的值
		 * 所以在泛型中没有父类与子类之分,所以要用下面的方法
		 */
		ArrayList<Object> test2 = new ArrayList<Object>();
		test.add("a");
		test.add("b");
		printCollection(test2);
		
		String[] strs = new String[]{"aaa","bbb","ccc"};
		ArrayList lists = new ArrayList();
		lists = (ArrayList) copy1(strs, lists);
		for (Object list : lists) {
			System.out.println(list);
		}

//		泛型的追溯特性,String和Date的都是Serializable的子类
		copy(new String[10], new Date[10]);
		
//      泛型的传播特性,其中为Date类型()
//		copy1(new String[10],new ArrayList<Date>());
	}

	/*为此推出了?这个通配符*/
	public static void printCollection2 (Collection<?> collection){
		/*但是引入这个通配符后,不能对这样的Collection对象去调用与参数有关的方法*/
		/*collection.add("aa");这样的语句是报错的,因为他不能确定泛型是什么类型的*/
		for(Object c : collection){
			System.out.println(c);
		}
	}
	//13:servlet jsp jmail jdbc jndi xml 
	public static void ClassTest(){
		Class<?> a = null;
	}
	// 泛型方法	
	public static <T> T[] copy(T[] forms,T[] to){
		return null;
	}
	
	public static <T> Collection<T> copy1(T[] froms,Collection<T> to){
		for(T yuan : froms){
			to.add(yuan);
		}
		return to;
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值