java集合框架: 泛型
泛型
1.概述
-
泛型是一种未知的数据类型,当我们不知道使用什么数据类型的时候,可以使用泛型
-
泛型也可以看成是一个变量,用来接收数据类型
E e:Element元素
T t:Type类型
-
创建集合对象的时候,就会确定泛型的数据类型,把数据类型作为参数传递,赋值给泛型E
2. 泛型的定义与使用
-
创建集合对象,不使用泛型:
- 好处:默认类型是Object类型,可以存储任意类型的数据
- 弊端:不安全,会引发异常
-
使用泛型:
- 好处:1.避免了类型转换的麻烦;
2.把运行期(运行之后抛出异常)异常提升到了编译期(写代码时会报错) - 弊端:只能存储泛型类型的数据
- 好处:1.避免了类型转换的麻烦;
定义一个含有泛型的类
- 模拟ArrayList集合
//含有泛型的类
public class GenericClass<E> {
private E name;
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
}
//测试
public class Demo01 {
public static void main(String[] args) {
//不写泛型默认为Object类型
GenericClass gc=new GenericClass();
//创建GenericClass对象,泛型使用Integer类型
GenericClass<Integer> gc2=new GenericClass<>();
gc2.setName(1);
Integer name = gc2.getName();
//创建GenericClass对象,泛型使用String类型
GenericClass<String> gc3=new GenericClass<>();
gc3.setName("aa");
String name1 = gc3.getName();
}
}
定义含有泛型的方法
- 泛型定义在方法的修饰符和返回值类型之间
- 定义格式:
修饰符 <代表泛型的变量> 返回值类型 方法名(参数){} - 在调用方法的时候确定泛型的数据类型,传递什么类型的数据,泛型就是什么类型
public class GenericMethod {
//含有泛型的方法
public <M> void method01(M m){
System.out.println(m);
}
//含有泛型的静态方法
public static <S> void method02(S s){
System.out.println(s);
}
}
//测试
public class Demo01 {
public static void main(String[] args) {
//创建对象
GenericMethod gm=new GenericMethod();
//调用方法
gm.method01(10);
gm.method01("aaa");
gm.method01(true);
gm.method02("静态方法,不建议创建对象使用");
GenericMethod.method02("静态方法通过【类名.方法名(参数)】直接使用");
}
}
定义含有泛型的接口
-
定义格式:
修饰符 interface接口名<代表泛型的变量>{} -
使用方式:
-
定义接口的实现类,实现接口,指定接口的泛型
//Scanner类实现了Iterator接口,并指定接口的泛型为String,所以重写的next方法泛型默认就是字符串 public interface Iterator<E>{//有泛型的接口 E next(); } public final class Scanner implements Iterator<String>{ public String next(){} } -
接口使用什么泛型,实现类就使用什么泛型,类跟着接口走;就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
//比如: public interface list<E>{ boolean add(E e); E get(int index); } public class ArrayList<E> implements List<E>{ public boolean add(E e){}; public E get(int index){} }
-
//含有泛型的接口
public interface GenericInterface<I> {
public abstract void method(I i);
}
//含有泛型的接口
public interface GenericInterface<I> {
public abstract void method(I i);
}
//实现类 方式一: 指定接口的泛型
public class GenericInterfaceImpl1 implements GenericInterface<String>{
@Override
public void method(String s) {
System.out.println(s);
}
}
//实现类 方式二:不指定接口泛型的类型
public class GenericInterfaceImpl2<I> implements GenericInterface<I>{
@Override
public void method(I i) {
System.out.println(i);
}
}
//测试类
public class Demo01 {
public static void main(String[] args) {
//创建实现类对象
//方式一
GenericInterfaceImpl1 gi1=new GenericInterfaceImpl1();
gi1.method("字符串");
//方式二:创建对象的时候确定泛型的类型
GenericInterfaceImpl2<Integer> gi2=new GenericInterfaceImpl2<>();
gi2.method(10);
GenericInterfaceImpl2<Double> gi3=new GenericInterfaceImpl2<>();
gi3.method(1.1);
}
}
3. 泛型通配符
- 当使用泛型类或接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示
- ?:代表任意的数据类型
- 一旦使用泛型通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用
- 只能接收数据,不能往该集合中存储数据
- 不能创建对象使用,只能作为方法的参数使用
public class Demo01 {
public static void main(String[] args) {
ArrayList<Integer> list01= new ArrayList<>();
list01.add(1);
list01.add(2);
ArrayList<String> list02= new ArrayList<>();
list02.add("a");
list02.add("b");
prinArray(list01);
prinArray(list02);
//ArrayList<?> list03= new ArrayList<?>(); 定义的时候不能使用通配符
}
//方法:能遍历所有类型的ArrayList集合
//注:泛型没有继承概念,不能写<Object>
public static void prinArray(ArrayList<?> list){
//使用迭代器遍历
Iterator<?> it = list.iterator();
while (it.hasNext()){
//取出的元素是Object类型,只有Object类型可以接收任意的数据类型
Object next = it.next();
System.out.println(next);
}
}
}
泛型通配符的高级使用---受限泛型
-
可以指定一个泛型的上限和下限
-
泛型的上限:
- 格式:
类型名称 <? extends 类> 对象名称 - 意义:只能接收该类型及其子类
- 格式:
-
泛型的下限:
- 格式:
类型名称 <? super 类> 对象名称 - 意义:只能接收该类型及其父类型
- 格式:
public class Demo01 {
public static void main(String[] args) {
Collection<Integer> list1=new ArrayList<Integer>();
Collection<String> list2=new ArrayList<String>();
Collection<Number> list3=new ArrayList<Number>();
Collection<Object> list4=new ArrayList<Object>();
getElement1(list1);
getElement1(list2);//报错
getElement1(list3);
getElement1(list4);//报错
getElement2(list1);//报错
getElement2(list2);//报错
getElement2(list3);
getElement2(list4);
/*
类与类之间的继承关系:
Integer extends Number extends Object
String extends Object
*/
}
//泛型的上限:此时的泛型?,必须是Number类型或者其子类型
public static void getElement1(Collection<? extends Number> coll){}
//泛型的下限:此时的泛型?,必须是Number类型或者其父类型
public static void getElement2(Collection<? super Number> coll){}
}

浙公网安备 33010602011771号