java集合框架---泛型设计和出现

泛型的概念

泛型概念:在JDK1.5以后出现新特性。用于解决安全问题,是一个类型安全机制。

    

    好处:

    1、将运行时期出现问题ClassCastException,转移到了编译时期,
       方便于程序员解决问题。让运行产生的类转换异常出现在编译时期。
    2、避免了强制转换麻烦
    

    泛型的格式

     通过<>来定义要操作的引用数据类型。
     在使用java提供的对象时,通常什么时候写泛型呢?
     通常在集合框架中很常见

     只要见到<>就要定义泛型。

import java.util.*;
public class GenericDemo{
 public static void main(String []args){
		ArrayList al = new ArrayList();
		al.add("name");
		al.add("name1");
		al.add(1);  //添加一个不是同一类型的元素,编译可以通过,执行出现异常。
		Iterator it= al.iterator();
		while(it.hasNext()){
			String str = (String)it.next();
			sop(str);
		}
	}
	public static void sop(Object obj){
			System.out.println(obj);
		}
}

存在的异常图片:

自定义泛型类

    问题:什么时候定义泛型类
       当引用数据类型不确定的时候,需要定义泛型类
       早期是使用Object类来扩展,
       之后使用泛型类来扩展。

import java.util.*;
class Student{
	
}
class Worker{
	
}
class Utils<T>{
	private T t;
	public void setT(T t){
		this.t = t;
	}
	public T getT(){
		return t;
	}
}
public class GenericDemo2{
	public static void main(String []args){
		Utils<Student> util = new Utils<Student>();
		util.setT(new Student());
		Student s = util.getT();
	}
}

泛型定义在方法上:

当一个方法上的数据引用不确定时,可以使用泛型方法。
注意:
 静态方法不能使用类中的泛型,泛型必须定义在静态方法上,因为无法从静态上下文中引用非静态类型。
 定义格式 static +<T>+返回值类型 +方法名

import java.util.*;
class Demo<T>
{
	public <T> void show(T t){
		System.out.println("show:"+t);
	}
	public static <T> void print(T q){
		System.out.println("print:"+q);
	}
}
public class GenericDemo3{
	public static void main(String []args){
		Demo<String> d = new Demo<String>();
		d.show(4);
		d.show("hhhh");
		Demo.print(534);
	}
}

泛型定义在接口上:

import java.util.*;
interface Inter<T>{
	void show(T t);
}
//第一种定义方法,已确定实现类的引用类型
class InterImpl implements Inter<String>{
	public void show(String s){
		System.out.println("show1:"+s);
	}
}
//第二种定义方法,未确定实现类的引用类型
class Inter2Impl<T> implements Inter<T>{
	public void show(T t){
		System.out.println("show2:"+t);
	}
}
public class GenericDemo3{
	public static void main(String []args){
	  Inter<Integer> ii2 = new Inter2Impl<Integer>();
	  ii2.show(54);
	  Inter<String> ii = new InterImpl();
	  ii.show("my name is lzl");
	}
}

泛型的高级应用:

 ? 表示通配符,也可以叫做占位符。
? extends E: 可以接收E类型或者E的子类型。上限
 ? super E: 可以接收E类型或者E的父类型。下限

import java.util.*;
class Student extends Person{
  static{
	  System.out.println("Student....");
  }
  public Student(String name){
	  super(name);
  }
}
class Person{
	private String name;
	public Person(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
}
public class GenericDemo4{
	public static void main(String []args){
	  ArrayList<String> al = new ArrayList<String>();
	  al.add("name1");
	  al.add("name2");
	  al.add("name3");
	  sop(al);
	  ArrayList<Integer> al1 = new ArrayList<Integer>();
	  al1.add(1);
	  al1.add(2);
	  al1.add(3);
	  sop(al1);
	  ArrayList<Person> al2 = new ArrayList<Person>();
	  al2.add(new Student("hhh1"));
	  al2.add(new Student("hhh2"));
	  al2.add(new Student("hhh3"));
	  al2.add(new Person("xixix1"));
	  al2.add(new Person("xixix1"));
	  arrayColl(al2);
	}
	/*
	用于打印未知引用类型的数据。?表示接收任意类型的对象
	*/
	public static void sop(ArrayList<?> al){
		Iterator<?> it = al.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
	/*
	泛型的限定.
	  <? extends Person> 表示可以接收Person及Person的子类对象。
	*/
	public static void arrayColl(ArrayList<? extends Person> al){
		Iterator<? extends Person> it = al.iterator();
		while(it.hasNext()){
			System.out.println(it.next().getName());
		}
	}
}
import java.util.*;
class Student extends Person{
  public Student(String name){
	  super(name);
  }
}
class Person{
	private String name;
	public Person(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
}
class Worker extends Person{
  public Worker(String name){
	  super(name);
  }
}
public class GenericDemo5{
	public static void main(String []args){
	  TreeSet<Person> al2 = new TreeSet<Person>(new Comp());
	  al2.add(new Student("hhh1"));
	  al2.add(new Student("hhh2"));
	  al2.add(new Student("hhh3"));
	  al2.add(new Person("xixix1"));
	  al2.add(new Person("xixix1"));
	  arrayColl(al2);
	}
	/*
	泛型的限定.
	  <? extends Person> 表示可以接收Person及Person的子类对象。
	*/
	public static void arrayColl(TreeSet<Person> al){
		Iterator<? extends Person> it = al.iterator();
		while(it.hasNext()){
			System.out.println(it.next().getName());
		}
	}
}
class Comp implements Comparator<Person>{
	public int compare(Person o1,Person o2){
		return o1.getName().compareTo(o2.getName());
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值