java基础(6)-集合类2

泛型

泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型,参数化类型,把类型当做参数一样的传递

好处:
1)把运行时期的问题提前到了编译器期间
2)避免了强制类型转换
3)优化了程序设计,解决了黄色警告线

例1:用ArrayList存储字符串元素,并遍历,用泛型改进

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo1{
	public static void main(String[] args){
		ArrayList<String> a1=new ArrayList<String>();
		a1.add("hadoop");
		a1.add("spark");
		a1.add("storm");

		Iterator<String> it = a1.iterator();
		while(it.hasNext()){
			String s = it.next();
			System.out.println(s);
		}
		
		System.out.println("----------------for--------------------");
		for(int i=0;i<a1.size();i++){
			String s = a1.get(i);
			System.out.println(s);
		}
	}
}

--------------------------------------------------------
输出结果
hadoop
spark
storm
---------------for----------------------------
hadoop
spark
storm
---------------------------------------------

例2:用ArrayList存储自定义对象,并遍历,用泛型改进

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo2{
	public static void main(String[] args){
		ArrayList<Student> a1 = new ArrayList<Student>();
	
		Student s1 = new Student("wu",24);
		Student s2 = new Student("sun",25);
		Student s3 = new Student("xu",26);

		a1.add(s1);
		a1.add(s2);
		a1.add(s3);

		Iterator<Student> it=a1.iterator();
		while(it.hasNext()){
			Student s = it.next();
			System.out.println(s);
		}

		System.out.println("---------for----------------------");
		for(int i=0;i<a1.size();i++){
			System.out.println(a1.get(i));
		}
	}
}

class Student{
	private int age;
	private String name;

	//构造方法
	public Student(){
		super();
	}

	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}

	public void setName(String name){
		this.name = name;
	}

	public String getName(){
		return name;
	}

	public void age(int age){
		this.age = age;
	}

	public int age(){
		return age;
	}

	public String toString(){
		return "Student [name="+name+",age="+age+"]";
	}
}

--------------------------------------------
输出结果
Student [name=wu,age=24]
Student [name=sun,age=25]
Student [name=xu,age=26]
---------for----------------------
Student [name=wu,age=24]
Student [name=sun,age=25]
Student [name=xu,age=26]



  • 泛型类

把泛型定义在类上
格式:public class 类名<泛型类型1,...>
注意:泛型类型必须是引用类型

public class ObjectToolDemo{
	public static void main(String[] args){
/*		
		ObjectTool ot = new ObjectTool();
		ot.setObj(new String("wujiadong"));
		String s =(String) ot.getObj();
		System.out.println(s);
	
		ot.setObj(new Integer(30));
		Integer i=(Integer) ot.getObj();	
		System.out.println(i);
*/

		ObjectTool<String> ot1 = new ObjectTool<String>();
		ot1.setObj(new String("wujiadong"));
		String s = ot1.getObj();
		System.out.println(s);

		ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
		ot2.setObj(new Integer(2));
		Integer i = ot2.getObj();
		System.out.println(i);
		
	}
}

/*
 *泛型类:把泛型定义在类上
 */
class ObjectTool<T> {
	private T obj;

	public T getObj(){
		return obj;
	}	
 	
	public void setObj(T obj){
		this.obj = obj;
	}
}

-------------------------------------------
输出结果
wujiadong
2
----------------------------------------------
  • 泛型方法

把泛型定义在方法上
格式:public <泛型类型> 返回类型 方法名(泛型类型){}

前示例:

public class ObjectToolDemo1{
	public static void main(String[] args){
/*
		ObjectTool ot = new ObjectTool();
		ot.show("wujiadong");
		ot.show(10);
		ot.show(true);
*/
		ObjectTool<String> ot1 = new ObjectTool<String>();
		ot1.show("wujiadong");

		ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
		ot2.show(20);

		ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
		//大写的Boolean,小写的boolean是基本数据类型,而这里只能是引用数据类型
		ot3.show(true);
				
	}
}

class ObjectTool<T>{
/*
	public void show(String s){
		System.out.println(s);
	}
	
	public void show(Integer i){
		System.out.println(i);
	}

	public void show(Boolean b){
		System.out.println(b);
	}
*/

	public void show(T t){
		System.out.println(t);
	}
}

------------------------------------------

输出结果
wujiadong
20
true
-----------------------------------------

泛型方法示例

public class ObjectToolDemo1{
	public static void main(String[] args){

		ObjectTool ot = new ObjectTool();
		ot.show("wujiadong");
		ot.show(10);
		ot.show(true);


/*
		ObjectTool<String> ot1 = new ObjectTool<String>();
		ot1.show("wujiadong");

		ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
		ot2.show(20);

		ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
		//大写的Boolean,小写的boolean是基本数据类型,而这里只能是引用数据类型
		ot3.show(true);
*/				
	}
}

class ObjectTool{
/*
	public void show(String s){
		System.out.println(s);
	}
	
	public void show(Integer i){
		System.out.println(i);
	}

	public void show(Boolean b){
		System.out.println(b);
	}
*/


	public <T>void show(T t){
		System.out.println(t);
	}
}

--------------------------------------
输出结果
wujiadong
10
true
---------------------------------------

  • 泛型接口

把泛型定义在接口上
格式:public interface 接口名<泛型类型1,...>

//接口
interface Inter<T>{
	public abstract void show(T t);//接口中抽象方法无方法体

} 

//实现接口
//实现类实现接口分两种情况:
//第一:已经知道是什么类型
//第二:不知奥是什么类型
class InterImp1 implements Inter<String>{
	public void show(String t){
		System.out.println(t);
	}

}

public class InterDemo{
	public static void main(String[] args){
		//InterImp1 i = new InterImp1();
		Inter<String> i = new InterImp1();
		i.show("wujiadong");
		
	}
}
-------------------------------------------
输出结果
wujiadong
-------------------------------------------

第二种情况(常用)

//接口
interface Inter<T>{
	public abstract void show(T t);//接口中抽象方法无方法体

} 

//实现接口
//实现类实现接口分两种情况:
//第二:不知奥是什么类型
class InterImp1<T> implements Inter<T>{
	public void show(T t){
		System.out.println(t);
	}

}

public class InterDemo{
	public static void main(String[] args){
		//InterImp1 i = new InterImp1();
		Inter<String> i = new InterImp1<String>();
		i.show("wujiadong");

		Inter<Integer> i1 = new InterImp1<Integer>();
		i1.show(10);
		
		Inter<Boolean> i2 = new InterImp1<Boolean>();
		i2.show(true);
	}
}


----------------------------------------------
输出结果
wujiadong
10
true

泛型高级(通配符)

1)泛型通配符<?>

  • 任意类型,如果没有明确,那么就是Object以及任意的java类了

2)?extend E

  • 向下限定,E及其子类

3)?super E

  • 向上限定,E及其父类

举例说明

import java.util.Collection;
import java.util.ArrayList;
class Animal{

}

class Dog extends Animal{
	
}

class Cat extends Animal{

}

public class GenericDemo{
	public static void main(String[] args){
		//泛型如果明确的写的时候,前后必须一致
//		Collection<Object> c = new ArrayList<Animal>();错误的写法

		//?表示任意的类型都是可以的
		Collection<?> c1 = new ArrayList<Object>();
		Collection<?> c2 = new ArrayList<Animal>();
		Collection<?> c3 = new ArrayList<Dog>();
		Collection<?> c4 = new ArrayList<Cat>();

		//?extends E 向下限定,E及其子类
		//Collection<? extends Animal> c5 = new ArrayList<Object>();报错
		Collection<? extends Animal> c5 = new ArrayList<Animal>();
		Collection<? extends Animal> c6 = new ArrayList<Dog>();
		Collection<? extends Animal> c7 = new ArrayList<Cat>();
	
		//?super E 向上限定,E及其父类
		Collection<? super Animal> c8 = new ArrayList<Object>();
        Collection<? super Animal> c9 = new ArrayList<Animal>();
        //Collection<? super Animal> c10 = new ArrayList<Dog>();
        //Collection<? super Animal> c11 = new ArrayList<Cat>();
	}
}


posted @ 2016-11-03 22:15  邬家栋  阅读(205)  评论(0编辑  收藏  举报