韩顺平Java学习笔记_泛型专题

一 泛型语法

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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值