一 泛型语法
1 泛型的介绍
2 泛型的声明
3 泛型的实例化
4 泛型使用举例
new Student("lw", 8);
HashSet<Student> set = new HashSet<Student>();
set.add(new Student("xm", 21));
set.add(new Student("wa", 12));
set.add(new Student("2ee", 21));
for (Student std:
set) {
System.out.println(std);
}
HashMap<String,Student> map = new HashMap<String,Student>();
map.put("xm",new Student("xm", 21));
map.put("mm",new Student("mm", 23));
map.put("ws",new Student("ws", 3));
//keyset -> iterator遍历
Set<String> stringSet = map.keySet();
Iterator<String> iterator = stringSet.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
System.out.println(map.get(next));
}
System.out.println("===");
//entrySet -> foreach遍历
for (Map.Entry<String, Student> entry : map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
5 泛型使用的注意事项和细节
6 练习题
普通版:
Employee haha = new Employee("haha", 9333, new MyDate(2012, 3, 23));
Employee hehe = new Employee("hehe", 94544, new MyDate(2012, 3, 23));
Employee xixi = new Employee("xixi", 34534, new MyDate(2012, 3, 23));
ArrayList<Employee> list = new ArrayList<>();
list.add(haha);
list.add(hehe);
list.add(xixi);
list.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if (!(o1 instanceof Employee && o2 instanceof Employee)) {
System.out.println("wrong type");
return 0;
}
int res = o1.getName().compareTo(o2.getName());
if (res != 0) {
return res;
}
if (o1.getBirthday().getYear() != o2.getBirthday().getYear()) {
return o1.getBirthday().getYear() - o2.getBirthday().getYear();
}
if (o1.getBirthday().getMonth() != o2.getBirthday().getMonth()) {
return o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
}
return o1.getBirthday().getDay() - o2.getBirthday().getDay();
}
});
改进版:
//省略部分重复代码
list.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if (!(o1 instanceof Employee && o2 instanceof Employee)) {
System.out.println("wrong type");
return 0;
}
int res = o1.getName().compareTo(o2.getName());
if (res != 0) {
return res;
}
return o1.getBirthday().compareTo(o2.getBirthday());
}
});
//改写Mydate类的compareTo方法,把比较年月日的逻辑放进去
@Override
public int compareTo(MyDate o) {//把对年月日的比较放在这里
if (year != o.getYear()) {
return year - o.getYear();
}
if (month != o.getMonth()) {
return month - o.getMonth();
}
return day - o.getDay();
}
Note:注意比较两者差别,理解编程思想!!!
二 自定义泛型
1 泛型类
//1. Tiger 后面泛型,所以我们把Tiger称为自定义泛型类 //2. T, R, M 泛型的标识符,一般都是单个大写字母 //3. 泛型的标识符可以有多个 //4. 普通成员可以使用泛型(属性,方法) //5. 使用泛型的数组不能初始化,因为数组在new的时候不能确定类型,就无法在内存开空间 //6. 静态方法不能使用泛型,因为静态是和类相关的,在类加载时,对象还没有创建,所以 如果静态方法和静态属性使用了泛型, JVM就无法完成初始化
2 泛型接口
3 泛型方法
三 泛型继承和通配符
四 练习题
public class DAO <T> {
private Map<String, T> map;
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.replace(id, map.get(id), entity);
}
public List<T> list(){
ArrayList<T> list = new ArrayList<>();
for (T entity : map.values()){
list.add(entity);
}
return list;
}
public void delete(String id){
map.remove(id);
}
}