示例代碼:
package com.genericity;import org.junit.Test;/*** @Title: LinkedListStack.java * @Package com.genericity * @Description: 編寫一個泛型棧(鏈表式)* @author lky * @date 2015年10月17日 下午8:34:07 * @version V1.0 */public class LinkedListStack<T> { /** * @Title: LinkedListStack.java * @Package com.genericity * @Description:定義棧中的節點類型 * @author lky * @date 2015年10月17日 下午8:38:51 * @version V1.0 */ PRivate static class Node<U>{ U item; Node<U> next; Node(){ this.item=null; this.next=null; } Node(U item,Node<U>next){ this.item=item; this.next=next; } boolean isEmpty(){ return item==null && next==null; } } private Node<T> top=new Node<T>();//棧頂指針 public void push(T item){ //入棧 top=new Node<T>(item,top); } public T pop(){ //出棧 T result=top.item; if(!top.isEmpty()){ top=top.next; } return result; } }
測試:
package com.genericity;import org.junit.Test;public class testLinkedListStack { @Test public void testPush(){ LinkedListStack<String> aLinkedListStack=new LinkedListStack<String>(); aLinkedListStack.push("lky"); aLinkedListStack.push("aaaa"); String res=aLinkedListStack.pop(); while(res!=null){ System.out.println(res); res=aLinkedListStack.pop(); } }}
package com.genericity;import java.util.ArrayList;import java.util.Date;import org.junit.Test;public class GenericMethods { /** * @Title: getType * @Description: 返回任意數組的數據類型 * @param item */ public <T> String getType(T item){ return item.getClass().getName(); } @Test public void test(){ System.out.println(new GenericMethods().getType(new Date())); System.out.println(new GenericMethods().getType(1)); System.out.println(new GenericMethods().getType("lky")); System.out.println(new GenericMethods().getType(new ArrayList<String>())); }}
鑒于上述的問題,java5中引入了泛型機制,在定義集合容器對象時顯式指定其元素的數據類型,在使用集合容器時,編譯器會檢查數據類型是否和容器指定的數據類型相符合,如果不符合在無法編譯通過,從編譯器層面強制保證數據類型安全。
示例代碼:
package com.genericity;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Queue;import java.util.Set;import org.junit.Test;public class New { public <k, v> Map<k, v> map() { return new HashMap<k, v>(); } public <T> List<T> list() { return new ArrayList<T>(); } public <T> LinkedList<T> linkList() { return new LinkedList<T>(); } public <T> Set<T> set() { return new HashSet<T>(); } public <T> Queue<T> queue() { return new LinkedList<T>(); } @Test public void test(){ New new1=new New(); Map<String, LinkedList<String>> lisMap=new1.map(); }}
代碼實現:
package com.genericity;import java.util.HashSet;import java.util.Set;import org.junit.Test;public class Sets { /** * @Title: union * @Description: 集合的并集 * @throws */ public static <T> Set<T> union(Set<T> a,Set<T> b){ Set<T> set=new HashSet<T>(a); set.addAll(b); return set; } /** * @Title: intersetion * @Description: 集合交集 */ public static <T> Set<T> intersetion(Set<T>a,Set<T> b){ Set<T> set=new HashSet<T>(a); set.retainAll(b); return set; } /** * @Title: difference * @Description: 集合差集 */ public static <T> Set<T> difference(Set<T>a, Set<T> b){ Set<T> set=new HashSet<T>(a); set.removeAll(b); return set; } public static <T> Set<T> complement(Set<T>a,Set<T> b){ return difference(union(a, b), intersetion(a, b)); } @Test public void test(){ HashSet<Integer> a=new HashSet<Integer>(); HashSet<Integer> b=new HashSet<Integer>(); for(int i=0;i<8;++i){ if(i<5)a.add(i); if(i>2) b.add(i); } System.out.println(union(a, b).toString()); System.out.println(difference(a, b).toString()); System.out.println(intersetion(a, b).toString()); System.out.println(complement(a, b).toString()); } }
一個比較經典泛型通配符的例子如下:
public class SampleClass < T extends S> {…}
假如A,B,C,…Z這26個class都實現了S接口。我們使用時需要使用到這26個class類型的泛型參數。那實例化的時候怎么辦呢?依次寫下
SampleClass<A> a = new SampleClass();
SampleClass<B> a = new SampleClass();
…
SampleClass<Z> a = new SampleClass();
這顯然很冗余,還不如使用Object而不使用泛型,使用通配符非常方便:
SampleClass<? Extends S> sc = newSampleClass();
新聞熱點
疑難解答