技术问答-16

HashMap 续集

https://blog.csdn.net/harvic880925/article/details/50085595 可参考
comparableClassFor:

  /**
     * Returns x's Class if it is of the form "class C implements
     * Comparable<C>", else null.
     * 如果继承Comparable 就返回他的class 如果不是就返回null
     * 这个方法在putTreeVal的时候会用到 会把返回值传递给find方法 进行查询
     *
     */
    static Class<?> comparableClassFor(Object x) {
    	//x 是不是Comparable子类 
        if (x instanceof Comparable) {
        	//变量申明
            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
            //如果key是String类型 
            if ((c = x.getClass()) == String.class) // bypass checks
                return c;
            //获取普通类所继承的接口使用的是Class.getInterfaces()函数 
            // 获取泛型接口的对象 使用 getGenericInterfaces
            if ((ts = c.getGenericInterfaces()) != null) {
                for (int i = 0; i < ts.length; ++i) { 
                	//ParameterizedType代表一种泛型类型   public class Point<T> {} Point类就是泛型类型
                	//public class PointImpl extends Point<Integer> { }
                	//Class<?> clazz = PointImpl.class; Type type = clazz.getGenericSuperclass();
                	//获得PointImpl.class的父类,而它的父类是Point,这明显是一个泛型类型,所以它对应的类型就是ParameterizedType;
                	
                	//getRawType获取泛型表达式类或者接口的Class对象  上边的例子泛型是Point 所以返回Point.Class
                	//这里是判断返回的是不是Comparable.class
                	
                	//getActualTypeArguments 返回当前泛型表达式中,用来填充泛型变量的真正值的列表   上边例子就是Integer 
                    if (((t = ts[i]) instanceof ParameterizedType) &&
                        ((p = (ParameterizedType)t).getRawType() ==
                         Comparable.class) &&
                        (as = p.getActualTypeArguments()) != null &&
                        as.length == 1 && as[0] == c) // type arg is c
                        return c;
                }
            }
        }
        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值