泛型 代码笔记集合 day5 泛型

/**
* 泛型的使用
* 1.jdk 5.0新增的特性
* 2.在集合中使用泛型:
* 总结:
* ① 集合接口或集合类在jdk5.0时都修改为带泛型的结构。
* ② 在实例化集合类时,可以指明具体的泛型类型
* ③ 指明完以后,在集合类或接口中凡是定义类或接口时,内部结构(比如:方法、构造器、属性等)使用到类的泛型的位置,都指定为实例化的泛型类型。
* 比如:add(E e) --->实例化以后:add(Integer e)
* ④ 注意点:泛型的类型必须是类,不能是基本数据类型。需要用到基本数据类型的位置,拿包装类替换
* ⑤ 如果实例化时,没有指明泛型的类型。默认类型为java.lang.Object类型。
*
* 3.如何自定义泛型结构:泛型类、泛型接口;泛型方法。见 GenericTest1.java
*
* @author shkstart
* @create 2019 上午 9:59
*/
public class GenericTest {


//在集合中使用泛型之前的情况:
@Test
public void test1(){
ArrayList list = new ArrayList();
//需求:存放学生的成绩
list.add(78);
list.add(76);
list.add(89);
list.add(88);
//问题一:类型不安全
// list.add("Tom");

for(Object score : list){
//问题二:强转时,可能出现ClassCastException类型转换异常
int stuScore = (Integer) score;

System.out.println(stuScore);

}

}

//在集合中使用泛型的情况:以ArrayList为例
@Test
public void test2(){
ArrayList<Integer> list = new ArrayList<Integer>(); //泛型中不能用基本数据类型如int。。。 需要用它们的包装类:Integer。。。

list.add(78);
list.add(87);
list.add(99);
list.add(65);
//编译时,就会进行类型检查,保证数据的安全
// list.add("Tom");

//方式一:
// for(Integer score : list){
// //避免了强转操作
// int stuScore = score;
//
// System.out.println(stuScore);
//
// }
//方式二:
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
int stuScore = iterator.next();
System.out.println(stuScore);
}

}

//在集合中使用泛型的情况:以HashMap为例
@Test
public void test3(){
// Map<String,Integer> map = new HashMap<String,Integer>();
//jdk7新特性:类型推断
Map<String,Integer> map = new HashMap<>();

map.put("Tom",87);
map.put("Jerry",87);
map.put("Jack",67);

// map.put(123,"ABC");
//泛型的嵌套
Set<Map.Entry<String,Integer>> entry = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entry.iterator(); //Entry是Map内部的接口 不想加Map就直接导util包

while(iterator.hasNext()){
Map.Entry<String, Integer> e = iterator.next();
String key = e.getKey();
Integer value = e.getValue();
System.out.println(key + "----" + value);
}

}


}

集合中使用泛型的练习:

/**
* 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:

1). 使Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。

*
*/
public class EmployeeTest {

//问题二:按生日日期的先后排序。
@Test
public void test2(){

TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
//使用泛型以后的写法
@Override
public int compare(Employee o1, Employee o2) {
MyDate b1 = o1.getBirthday();
MyDate b2 = o2.getBirthday();

return b1.compareTo(b2);
}
//使用泛型之前的写法
//@Override
// public int compare(Object o1, Object o2) {
// if(o1 instanceof Employee && o2 instanceof Employee){
// Employee e1 = (Employee)o1;
// Employee e2 = (Employee)o2;
//
// MyDate b1 = e1.getBirthday();
// MyDate b2 = e2.getBirthday();
// //方式一:
//// //比较年
//// int minusYear = b1.getYear() - b2.getYear();
//// if(minusYear != 0){
//// return minusYear;
//// }
//// //比较月
//// int minusMonth = b1.getMonth() - b2.getMonth();
//// if(minusMonth != 0){
//// return minusMonth;
//// }
//// //比较日
//// return b1.getDay() - b2.getDay();
//
// //方式二:
// return b1.compareTo(b2);
//
// }
//// return 0;
// throw new RuntimeException("传入的数据类型不一致!");
// }
});

Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));

set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);

Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}


//问题一:使用自然排序
@Test
public void test1(){
TreeSet<Employee> set = new TreeSet<Employee>();

Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));

set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);

Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext()){
Employee employee = iterator.next();
System.out.println(employee);
}
}
}

自定义泛型:

视频: p566            代码在 java目录里面找 GenericTest1.java 和order.java    Suborder.java

★★★★★视频: p569 泛型类和泛型方法的使用情景。  代码在java1文件夹中 DAO为共性操作的父类,其余类都是继承它,再自定义泛型。

                       泛型方法和其所在的类中的泛型参数无关

泛型在继承方面的体现及通配符的使用:

/**
* 1. 泛型在继承方面的体现
* 2. 通配符的使用
* @author shkstart
* @create 2019 下午 2:13
*/
public class GenericTest {

/*
1. 泛型在继承方面的体现

虽然类A是类B的父类,但是G<A> 和G<B>二者不具备子父类关系,二者是并列关系。

补充:类A是类B的父类,A<G> 是 B<G> 的父类

*/
@Test
public void test1(){

Object obj = null;
String str = null;
obj = str;

Object[] arr1 = null;
String[] arr2 = null;
arr1 = arr2;
//编译不通过
// Date date = new Date();
// str = date;
List<Object> list1 = null;
List<String> list2 = new ArrayList<String>();
//此时的list1和list2的类型不具有子父类关系
//编译不通过
// list1 = list2;
/*
反证法:
假设list1 = list2;
list1.add(123);导致混入非String的数据。出错。

*/

show(list1);
show1(list2);

}

public void show1(List<String> list){

}

public void show(List<Object> list){

}

@Test
public void test2(){

AbstractList<String> list1 = null;
List<String> list2 = null;
ArrayList<String> list3 = null;

list1 = list3;
list2 = list3;

List<String> list4 = new ArrayList<>();

}

/*
2. 通配符的使用
通配符:?

类A是类B的父类,G<A>和G<B>是没有关系的,二者共同的父类是:G<?>


*/

@Test
public void test3(){
List<Object> list1 = null;
List<String> list2 = null;

List<?> list = null;

list = list1;
list = list2;
//编译通过
// print(list1);
// print(list2);


//
List<String> list3 = new ArrayList<>();
list3.add("AA");
list3.add("BB");
list3.add("CC");
list = list3;
//添加(写入):对于List<?>就不能向其内部添加数据。
//除了添加null之外。
// list.add("DD");
// list.add('?');

list.add(null);

//获取(读取):允许读取数据,读取的数据类型为Object。
Object o = list.get(0);
System.out.println(o);


}

public void print(List<?> list){
Iterator<?> iterator = list.iterator();
while(iterator.hasNext()){
Object obj = iterator.next();
System.out.println(obj);
}
}

/*
3.有限制条件的通配符的使用。
? extends A:
G<? extends A> 可以作为G<A>和G<B>的父类,其中B是A的子类

? super A:
G<? super A> 可以作为G<A>和G<B>的父类,其中B是A的父类

*/
@Test
public void test4(){

List<? extends Person> list1 = null;
List<? super Person> list2 = null;

List<Student> list3 = new ArrayList<Student>();
List<Person> list4 = new ArrayList<Person>();
List<Object> list5 = new ArrayList<Object>();

list1 = list3;
list1 = list4;
// list1 = list5;

// list2 = list3;
list2 = list4;
list2 = list5;

//读取数据:
list1 = list3;
Person p = list1.get(0);
//编译不通过
//Student s = list1.get(0);

list2 = list4;
Object obj = list2.get(0);
////编译不通过
// Person obj = list2.get(0);

//写入数据:
//编译不通过
// list1.add(new Student());

//编译通过
list2.add(new Person());
list2.add(new Student());

}

}

原文地址:https://www.cnblogs.com/wangyanbin2333/p/13293233.html