《Java编程思想》学习笔记7——泛型编程基础 .

一般的类和方法都是针对特定数据类型的,当写一个对多种数据类型都适用的类和方法时就需要使用泛型编程,java的泛型编程类似于C++中的模板,即一种参数化类型的编程方法,具体地说就是将和数据类型相关的信息抽象出来,主要提供通用的实现和逻辑,和数据类型相关的信息由使用时参数决定。

1.泛型类/接口:

(1).泛型接口:

如一个提供产生指定类的接口:

  1. public interface Gernerator<T>{  
  2.     T next() ;  
  3. }  
  4. public class A implement Generator<A>{  
  5.     A next(){  
  6.         return new A();  
  7. }  
  8. }  
public interface Gernerator<T>{
	T next() ;
}
public class A implement Generator<A>{
	A next(){
		return new A();
}
}

(2).泛型类:

一个使用泛型实现的栈数据结构如下:

  1. public class LinkedListStack<T>{  
  2.     //节点内部类   
  3.     private static class Node<U>{  
  4.         U item;  
  5.         Node<U> next;  
  6.         Node(){  
  7.             item = null;  
  8.             next = null;  
  9. }  
  10.         Node(U item, Node<U> next){  
  11.     this.item = item;  
  12.     this.next = next;  
  13. }  
  14. Boolean end(){  
  15.     return item == null && next == null;  
  16. }  
  17. }  
  18. private Node<T> top = new Node<T>();  
  19. public void push<T>(T item){  
  20.     top = new Node<T>(item, top);  
  21. }  
  22. public T pop(){  
  23.     T result = top.item;  
  24.     if(!top.end()){  
  25.         top = top.next();  
  26. }  
  27. return result;  
  28. }   
  29. }  
public class LinkedListStack<T>{
	//节点内部类
	private static class Node<U>{
		U item;
		Node<U> next;
		Node(){
			item = null;
			next = null;
}
		Node(U item, Node<U> next){
	this.item = item;
	this.next = next;
}
Boolean end(){
	return item == null && next == null;
}
}
private Node<T> top = new Node<T>();
public void push<T>(T item){
	top = new Node<T>(item, top);
}
public T pop(){
	T result = top.item;
	if(!top.end()){
		top = top.next();
}
return result;
} 
}

使用这个使用泛型实现的栈,可以操作各种数据类型。

2.泛型方法:

例如:

  1. public class GenericMethods{  
  2.     public <T> void f(T x){  
  3.         System.out.println(x.getClass().getName()) ;  
  4. }  
  5. public static void main(String[] args){  
  6.     GenericMethods gm = new GenericMethods();  
  7.     gm.f(“”);  
  8.     gm.f(1);  
  9.     gm.f(1.0);  
  10.     ……  
  11. }   
  12. }  
public class GenericMethods{
	public <T> void f(T x){
		System.out.println(x.getClass().getName()) ;
}
public static void main(String[] args){
	GenericMethods gm = new GenericMethods();
	gm.f(“”);
	gm.f(1);
	gm.f(1.0);
	……
} 
}

输出结果为:

java.lang.String

java.lang.Integer

java.lang.Double

3.泛型集合:

Java中泛型集合使用的非常广泛,在Java5以前,java中没有引入泛型机制,使用java集合容器时经常遇到如下两个问题:

a.       java容器默认存放Object类型对象,如果一个容器中即存放有A类型对象,又存放有B类型对象,如果用户将A对象和B对象类型弄混淆,则容易产生转换错误,会发生类型转换异常。

b.       如果用户不知道集合容器中元素的数据类型,同样也可能会产生类型转换异常。

鉴于上述的问题,java5中引入了泛型机制,在定义集合容器对象时显式指定其元素的数据类型,在使用集合容器时,编译器会检查数据类型是否和容器指定的数据类型相符合,如果不符合在无法编译通过,从编译器层面强制保证数据类型安全。

(1).java常用集合容器泛型使用方法:

如:

  1. public class New{  
  2.     public static <K, V> Map<K, V> map(){  
  3.         return new HashMap<K, V>();  
  4. }  
  5. public static <T> List<T> list(){  
  6.     return new ArrayList<T>() ;  
  7. }  
  8. public static <T> LinkedList<T> lList(){  
  9.     return new LinkedList<T>();  
  10. }  
  11. public static <T> Set<T> set(){  
  12.     return new HashSet<T>();  
  13. }  
  14. public static <T> Queue<T> queue(){  
  15.     return new LinkedList<T>() ;  
  16. }  
  17. ;public static void main(String[] args){  
  18.     Map<String, List<String>> sls = New.map();  
  19.     List<String> ls = New.list();  
  20.     LinkedList<String> lls = New.lList();  
  21.     Set<String> ss = New.set();  
  22.     Queue<String> qs = New.queue();  
  23. }  
  24. }  
public class New{
	public static <K, V> Map<K, V> map(){
		return new HashMap<K, V>();
}
public static <T> List<T> list(){
	return new ArrayList<T>() ;
}
public static <T> LinkedList<T> lList(){
	return new LinkedList<T>();
}
public static <T> Set<T> set(){
	return new HashSet<T>();
}
public static <T> Queue<T> queue(){
	return new LinkedList<T>() ;
}
;public static void main(String[] args){
	Map<String, List<String>> sls = New.map();
	List<String> ls = New.list();
	LinkedList<String> lls = New.lList();
	Set<String> ss = New.set();
	Queue<String> qs = New.queue();
}
}

(2).Java中的Set集合是数学上逻辑意义的集合,使用泛型可以很方便地对任何类型的Set集合进行数学运算,代码如下:

  1. public class Sets{  
  2.     //并集   
  3.     public static <T> Set<T> union(Set<T> a, Set<T> b){  
  4.         Set<T> result = new HashSet<T>(a);  
  5.         result.addAll(b);  
  6.         return result;  
  7. }  
  8. //交集   
  9. public static <T> Set<T> intersection(Set<T> a, Set<T> b){  
  10.     Set<T> result = new HashSet<T>(a);  
  11.     result.retainAll(b);  
  12.     return result;  
  13. }  
  14. //差集   
  15. public static <T> Set<T> difference(Set<T> a, Set<T> b){  
  16.     Set<T> result = new HashSet<T>(a);  
  17.     result.removeAll(b);  
  18.     return Result;  
  19. }  
  20. //补集   
  21. public static <T> Set<T> complement(Set<T> a, Set<T> b){  
  22.     return difference(union(a, b), intersection(a, b));  
  23. }  
  24. }  
public class Sets{
	//并集
	public static <T> Set<T> union(Set<T> a, Set<T> b){
		Set<T> result = new HashSet<T>(a);
		result.addAll(b);
		return result;
}
//交集
public static <T> Set<T> intersection(Set<T> a, Set<T> b){
	Set<T> result = new HashSet<T>(a);
	result.retainAll(b);
	return result;
}
//差集
public static <T> Set<T> difference(Set<T> a, Set<T> b){
	Set<T> result = new HashSet<T>(a);
	result.removeAll(b);
	return Result;
}
//补集
public static <T> Set<T> complement(Set<T> a, Set<T> b){
	return difference(union(a, b), intersection(a, b));
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值