Java泛型
泛型
看一个需求
使用传统方法的问题
- 不能对加入到集合ArrayList中的数据类型进行约束(不安全)
- 遍历的时候,需要进行类型转换,如果集合中的数据量较大,对效率有影响
泛型的好处
- 编译时,检查添加元素的类型,提高了安全性
- 减少了类型转换的次数,提高效率
代码示例
import java.util.ArrayList;
/**
* @author wty
* @date 2022/9/26 19:54
*/
@SuppressWarnings({"all"})
public class Main {
public static void main(String[] args) {
// 传统方法
ArrayList arrayList = new ArrayList();
Dog dog = new Dog();
dog.setAge(10);
dog.setName("1号狗狗");
Dog dog1 = new Dog();
dog1.setAge(20);
dog1.setName("2号狗狗");
Dog dog2 = new Dog();
dog2.setAge(30);
dog2.setName("3号狗狗");
arrayList.add(dog.getAge());
arrayList.add(dog.getName());
arrayList.add(dog1.getAge());
arrayList.add(dog1.getName());
arrayList.add(dog2.getAge());
arrayList.add(dog2.getName());
System.out.println(arrayList);
System.out.println("增强for循环:");
// 增强for循环
ArrayList arrayList1 = new ArrayList();
arrayList1.add(new Dog("旺财", 10));
arrayList1.add(new Dog("小黄", 20));
arrayList1.add(new Dog("小绿", 30));
// 加入增加了一只猫(运行时报错) arrayList1是Dog,这里变成了Cat
//arrayList1.add(new Cat("大橘", 40));
for (Object o : arrayList1) {
Dog dog3 = (Dog) o;
System.out.println(dog3.getName() + dog3.getAge());
// System.out.println(dog3.getAge() + dog3.getName());
}
System.out.println("泛型解决:");
// 泛型解决
// 1.当我们这样写,ArrayList<Dog>表示存放到ArrayList 集合中的元素是Dog类型
// 2.如果编译器发现添加的类型,不满足要求,就会报错
ArrayList<Dog> arrayList2 = new ArrayList<Dog>();
//(编译器报错)arrayList2.add(new Cat("大橘", 40));
arrayList2.add(new Dog("旺财泛型", 10));
arrayList2.add(new Dog("小黄泛型", 20));
arrayList2.add(new Dog("小绿泛型", 30));
for (Dog dog3 : arrayList2) {
System.out.println(dog3.getName() + dog3.getAge());
}
}
}
class Dog {
private String name;
private int age;
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Cat {
private String name;
private int age;
public Cat() {
}
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
泛型介绍
- (广泛的类型)泛型又称参数化类型,是JDK5.0出现的新特征,解决数据类型的安全性问题。
- 在类声明或者实例化时只要制定好需要的具体类型即可。
- 泛型可以保证在编译时没有发出警告,那么运行时就不会有ClassCastException异常。同时代码更加简洁、健壮。
- 泛型的作用:可以在类声明时通过一个标识表示类中的某个属性的类型,或者是某个方法的返回值类型,或者是参数类型
代码示例
import java.util.ArrayList;
/**
* @author wty
* @date 2022/9/26 19:54
*/
@SuppressWarnings({"all"})
public class Main {
Person<String> person = new Person<String>("小刚");
System.out.println("person.show:");
person.show();
// 特别强调:T 具体的数据类型在定义Person的时候就确定了.即:在编译期间,就知道T的类型
/** Person类,可以理解为
*
* class Person<String>{
* String name; // 该属性的数据类型在创建对象的时候定义
* public Person(String name){ // name也可以在参数中使用
* this.name = name;
* }
*
* public String t(){
* return name; //也可以在返回值中使用
* }
* }
*/
Person<Integer> person2 = new Person<Integer>(100);
System.out.println("person2.show:");
person2.show();
/** Person类,可以理解为
*
* class Person<Integer>{
* Integer name; // 该属性的数据类型在创建对象的时候定义
* public Person(Integer name){ // name也可以在参数中使用
* this.name = name;
* }
*
* public Integer t(){
* return name; //也可以在返回值中使用
* }
* }
*/
}
}
class Person<T> {
T name; // 该属性的数据类型在创建对象的时候定义
public Person(T name) { // name也可以在参数中使用
this.name = name;
}
public T t() {
return name; //也可以在返回值中使用
}
public void show(){
System.out.println(name.getClass());
}
}
泛型的应用
泛型的声明
1.interface接口{} 和 class类<K,V>{} 比如List,ArrayList
(1)其中,T,K,V不代表值,而是表示类型
(2)任意字母都可以,通常用T表示,是Type的缩写
泛型的实例化
要在类名后面指定类型参数的值(类型)。例如:
(1)List< String > list = new ArrayList< String >();
(2)Iterator< Customer > iterator = customers.iterator();
泛型使用举例
package com.fanxing;
import java.util.*;
/**
* @author wty
* @date 2022/9/26 21:35
*/
public class Main {
public static void main(String[] args) {
HashSet<Student> student1 = new HashSet<Student>();
student1.add(new Student("小明", 10));
student1.add(new Student("小红", 20));
student1.add(new Student("小亮", 30));
for (Student o : student1) {
System.out.println(o);
}
// K → String V → Student
HashMap<String, Student> kvHashMap = new HashMap<String, Student>();
kvHashMap.put("小明", new Student("小明", 10));
kvHashMap.put("小红", new Student("小红", 20));
kvHashMap.put("小亮", new Student("小亮", 30));
// 迭代器遍历EntrySet()
System.out.println("迭代器:");
Set<Map.Entry<String, Student>> entries = kvHashMap.entrySet();
Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Student> next = iterator.next();
System.out.println(next.getKey() + "," + next.getValue());
}
}
}
class Student {
public String name;
public int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
泛型使用的注意事项和细节
1.interface List< T >{} public class HashSet< E >{}等
说明:T、E只能是引用类型(包装类)
List<Integer> list = new ArrayList<Integer>(); //√
List<int> list2 = new ArrayList<int>(); //×
2.在给泛型具体类型后,可以传入该类型或者其子类类型
3.泛型使用形式
List <Integer> list1 = new ArrayList <Integer>();
List <Integer> list2 = new ArrayList <>();
- 如果<>内什么也不写即省略,默认传的是Object
List list3 = new ArrayList ();
package com.zileiyingyong;
/**
* @author wty
* @date 2022/9/26 22:29
*/
public class Main {
public static void main(String[] args) {
Method<A> method = new Method<A>(new A());
method.getType();
// Method<A> method2 = new Method<A>(new B()); // 这样写会报错因为<A>指定了A类型
// 但是如果继承之后就不会报错了
Method<A> method3 = new Method<A>(new C());
method3.getType();
}
}
class A {
}
class B {
}
class C extends A {
}
class Method<T> {
T t;
public Method(T t) {
this.t = t;
}
public void getType() {
System.out.println(t.getClass()); // 运行类型
}
}
课堂练习
package com.HomeWork;
import org.junit.Test;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* @author wty
* @date 2022/11/8 12:16
*/
public class HomeWork01 {
@Test
public void HomeWork01Test(){
Employee employee1 = new Employee("小胖",10000,new MyDate("2000","03","15"));
Employee employee2 = new Employee("妖刀",12000,new MyDate("2002","01","11"));
Employee employee3 = new Employee("飞牛",32000,new MyDate("2003","03","12"));
Employee employee4 = new Employee("飞牛",32000,new MyDate("2003","02","12"));
List<Employee> list = new ArrayList<>();
list.add(employee1);
list.add(employee2);
list.add(employee3);
list.add(employee4);
list.sort(new Comparator<Employee>() {
@Override
public int compare(Employee emp1, Employee emp2) {
if (!(emp1 instanceof Employee && emp2 instanceof Employee)){
System.out.println("传入数据类型错误");
return 0;
}
int i = emp2.getName().compareTo(emp1.getName());
if (i == 0 ){
return (emp1.getBirthday().compareTo(emp2.getBirthday()));
}else {
return i;
}
}
});
for (Employee employee : list) {
System.out.println(employee);
}
}
}
class Employee{
private String name;
private double sal;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, double sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
}
// 这里实现了Comparable方法是关键
class MyDate implements Comparable<MyDate>{
private String year;
private String month;
private String day;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public MyDate(String year, String month, String day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public int compareTo(MyDate o) {
// 比较年
if(year.compareTo(o.getYear()) != 0){
return year.compareTo(o.getYear());
}
// 比较月
if(month.compareTo(o.getMonth()) != 0){
return month.compareTo(o.getMonth());
}
// 比较日
if(day.compareTo(o.getDay()) != 0){
return month.compareTo(o.getDay());
}
return 0;
}
@Override
public String toString() {
return "MyDate{" +
"year='" + year + '\'' +
", month='" + month + '\'' +
", day='" + day + '\'' +
'}';
}
}
泛型的使用形式
传统形式:
List< Integer > list1 = new ArrayList< Integer >();
简化形式: (推荐使用)
List< Integer > list2 = new ArrayList<>();
省略形式: 中T默认是Object类型
List list3 = new ArrayList();
示例:
package com.shili;
/**
* @author wty
* @date 2022/9/27 21:26
*/
public class Main {
public static void main(String[] args) {
// 如果这样写,默认是Object类型
Car car = new Car();
/**
*class Car {
* Object t;
*
* public Car() {
* }
*
* public Car(Object t) {
* this.t = t;
* }
* }
*/
}
}
class Car<T> {
T t;
public Car() {
}
public Car(T t) {
this.t = t;
}
}
自定义泛型
基本语法
class类名<T,R……>{
成员
}
注意细节:
- 普通成员方法可以使用泛型(属性、方法)
- 使用泛型的数组,不能初始化
- 静态方法中不能使用类的泛型
- 泛型类的类型,是在创建对象时确定的(因为创建对象时,需要指定确定类型)
- 如果在创建对象时没有指定类型,默认为Object
package com.zidingyi;
/**
* @author wty
* @date 2022/9/27 21:39
*/
@SuppressWarnings({"all"})
public class Main {
public static void main(String[] args) {
}
}
// 1. T后面有泛型,所以我们把T称为自定义泛型类
// 2. K,V,E是泛型的标识符,一般是大写字母
// 3. 泛型的标识符可以有多个
// 4. 普通成员(属性、方法)可以泛型
// 5. 使用泛型数组不能初始化,因为数组在new 不能确定K的类型,就无法开辟内存空间
// 6.静态方法不能使用泛型,因为静态是和类相关的,类加载的时候对象还没有创建,所以静态方法和静态属性使用了泛型,底层JVM无法完成初始化
@SuppressWarnings({"all"})
class T<K, V, E> {
K k;
V v;
E e;
// K[] array = new K[8]; // 报错;Type parameter 'K' cannot be instantiated directly
// 方法使用泛型
public void T() {
}
// 构造器使用泛型
public T(K k, V v, E e) {
this.k = k;
this.v = v;
this.e = e;
}
// 方法使用泛型
public K getK() {
return k; // 返回类型是泛型
}
public void setK(K k) {
this.k = k;
}
public V getV() {
return v;
}
public void setV(V v) {
this.v = v;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
// public static void m1(V v){} // 报错:'com.zidingyi.T.this' cannot be referenced from a static context
}
自定义泛型(类)练习
package com.zidingyi;
/**
* @author wty
* @date 2022/9/27 21:39
*/
@SuppressWarnings({"all"})
public class Main {
public static void main(String[] args) {
// K,V,R
T<Double, String, Integer> t = new T<>("John");
t.setK(10.9); // 正确
// t.setK("yy"); // 错误 Required type:
// Double
// Provided:
// String
System.out.println(t);
// 这里特别注意:使用了带参数的构造器
T t2 = new T("John~~"); // 正确
// 参数是String类型,赋予K就是String类型
t2.setK("yy");// 正确
System.out.println(t2);
}
}
@SuppressWarnings({"all"})
class T<K, V, E> {
K k;
V v;
E e;
String name;
// 方法使用泛型
public void T() {
}
public T(String name) {
this.name = name;
}
// 构造器使用泛型
public T(K k, V v, E e) {
this.k = k;
this.v = v;
this.e = e;
}
// 方法使用泛型
public K getK() {
return k; // 返回类型是泛型
}
public void setK(K k) {
this.k = k;
}
public V getV() {
return v;
}
public void setV(V v) {
this.v = v;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
@Override
public String toString() {
return "T{" +
"k=" + k +
", v=" + v +
", e=" + e +
", name='" + name + '\'' +
'}';
}
}
自定义泛型(接口)
基本语法
interface 接口名<T,R……>{}
注意细节:
(1)接口中,静态成员也不能使用泛型
(2)泛型接口的类型,在继承接口或者实现接口时确定
(3)没有指定类型,默认是Object
package com.fanxingjiekou;
/**
* @author wty
* @date 2022/9/27 22:15
*/
public class Main {
public static void main(String[] args) {
}
}
interface IUsb<U, R> {
// 抽象方法,可以使用接口
R get(U u);
void hi(R r);
void run(R r1, U u1, U u2);
// 默认方法可以使用泛型
default R method(U u) { // JDK 8可以用默认default修饰符
return null;
}
//public static final U u8; // 报错: 'com.fanxingjiekou.IUsb.this' cannot be referenced from a static context
int a = 10; // 这样可以的
}
// 在继承接口 指定泛型接口的类型
// String→U Integer→R
interface IB extends IUsb<String, Integer> {
}
// 当我们实现IB接口时,因为IB在继承IUsb接口时,指定了String → U Integer → R
// 在实现IUsb接口的方法时,会相应替换掉
class B implements IB {
@Override
public Integer get(String s) {
return null;
}
@Override
public void hi(Integer integer) {
}
@Override
public void run(Integer r1, String u1, String u2) {
}
}
// 同理换成别的类型,会相应替换掉
interface IC extends IUsb<Double, Float> {
}
class C implements IC {
@Override
public Float get(Double aDouble) {
return null;
}
@Override
public void hi(Float aFloat) {
}
@Override
public void run(Float r1, Double u1, Double u2) {
}
}
// 没有指定类型,默认为Object(不推荐这样使用)
class D implements IUsb {
@Override
public Object get(Object o) {
return null;
}
@Override
public void hi(Object o) {
}
@Override
public void run(Object r1, Object u1, Object u2) {
}
}
自定义泛型方法
基本语法
修饰符<T,r……>返回类型 方法名(参数列表){
}
注意细节
(1)泛型方法,可以定义在普通类中,也可以定义在泛型类中
(2)当泛型方法被调用时,类型会确定
(3)public void eat(E e){},修饰符后没有<T,R……> eat 方法不是泛型方法,而是使用了泛型
package com.fanxingfangfa;
/**
* @author wty
* @date 2022/9/27 22:36
*/
public class Main {
public static void main(String[] args) {
A a = new A();
a.fly("宝马", 100); // 当调用方法时传入了参数,编译器会确定对应的标识符类型
System.out.println("------------");
a.fly(100.12, 100);// 当调用方法时传入了参数,编译器会确定对应的标识符类型
System.out.println("------------");
Fish<String, ArrayList> fish = new Fish<>();
fish.walk(new ArrayList(), 100.3f);
}
}
// 泛型方法的使用
class A {
public void say() { // 普通方法
}
// 泛型方法
// 1.T,R是泛型标识符
// 2.提供给fly方法使用
public <T, E> void fly(T t, T r) {
System.out.println(t.getClass()); // String
System.out.println(r.getClass()); // Integer
}
}
// 泛型类
class Fish<T, R> {
// 泛型方法
public <Q, M> void eat(Q q, M m) {
}
// 这个方法drink不是泛型方法,仅仅只是使用了类声明的泛型
public void drink(T t) {
}
// 泛型方法,可以使用类声明的泛型R,也可以自己声明K
public <K> void walk(R r, K k){
System.out.println(r.getClass());
System.out.println(k.getClass());
}
}
泛型的继承和通配符
(1)泛型不具备继承性
List <Object> list = new ArrayList<String>() // 错误
(2)<?>: 支持任意泛型类型
(3)<? extend A>: 支持A类以及A类的子类,规定了泛型的上限
(4)<? super A>: 支持A类以及A类的父类,不限于直接父类,规定了泛型的下限
示例:
package com.fanxingextend;
import java.util.ArrayList;
import java.util.List;
/**
* @author wty
* @date 2022/9/27 22:59
*/
public class Main {
public static void main(String[] args) {
Object o = new String("可以"); // ✔
// 泛型没有继承性;报错
//List<Object> list = new ArrayList<String>(); // ❌
// 举例说明下面三个方法的使用
ArrayList<Object> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
ArrayList<AA> list3 = new ArrayList<>();
ArrayList<BB> list4 = new ArrayList<>();
ArrayList<CC> list5 = new ArrayList<>();
printCollection1(list1);
printCollection1(list2);
printCollection1(list3);
printCollection1(list4);
printCollection1(list5);
printCollection2(list3);
printCollection2(list4);
printCollection2(list5);
//printCollection2(list2); // 报错,String不是AA的子类
//printCollection2(list1); // 报错,Object不是AA的子类
printCollection3(list3);
//printCollection3(list4);// 报错,BB不是AA的父类
//printCollection3(list5);// 报错,BB不是AA的父类
printCollection3(list1);
//printCollection3(list2); // 报错,String不是AA的父类
}
// <?> 表示任意类型,l任意类型都可以接收
public static void printCollection1(List<?> l) {
for (Object o : l) {
System.out.println(o);
}
}
// ? extend AA 表示可以接收AA类或者AA的子类
public static void printCollection2(List<? extends AA> l) {
for (Object aa : l) {
System.out.println(aa);
}
}
// ? super AA 表示可以接收AA类或者AA的父类
public static void printCollection3(List<? super AA> l) {
for (Object o : l) {
System.out.println(o);
}
}
}
//爷爷
class AA {
}
//爸爸
class BB extends AA {
}
//儿子
class CC extends BB {
}
本章作业
package com.HomeWork;
import org.junit.Test;
import java.util.*;
/**
* @author wty
* @date 2022/11/8 15:38
*/
public class HomeWork04 {
@Test
public void HomeWork04Test(){
DAO<User> userDAO = new DAO<>();
userDAO.save("1001",new User(1001,10,"小红"));
userDAO.save("1002",new User(1002,20,"小张"));
List<User> list = userDAO.list();
for (User user : list) {
System.out.println(user);
}
User user = userDAO.get("1002");
System.out.println("1002号对象是:" + user);
userDAO.update("1002",new User(1002,20,"小华"));
userDAO.delete("1001");
list = userDAO.list();
for (User users : list) {
System.out.println(users);
}
}
}
class DAO<T>{
private Map<String,T> map = new HashMap<>();
public void save(String id,T entity){
map.put(id,entity);
}
public T get(String id){
return map.get(id);
}
public void update(String id,T entity){
map.put(id,entity);
}
public List<T> list(){
List<T> list = new ArrayList<>();
Set<Map.Entry<String, T>> entries = map.entrySet();
Iterator<Map.Entry<String, T>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, T> next = iterator.next();
list.add(next.getValue());
}
return list;
}
public void delete(String id){
map.remove(id);
}
}
class User{
private int id;
private int age;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}