为了账号安全,请及时绑定邮箱和手机立即绑定

Java TreeMap的put()方法中利用泛型创建接口对象?

Java TreeMap的put()方法中利用泛型创建接口对象?

叮当猫咪 2019-03-21 19:15:27
public V put(K key, V value) {        Entry<K,V> t = root;        if (t == null) {            compare(key, key); // type (and possibly null) check            root = new Entry<>(key, value, null);            size = 1;            modCount++;            return null;        }        int cmp;        Entry<K,V> parent;        // split comparator and comparable paths        Comparator<? super K> cpr = comparator;        if (cpr != null) {            do {                parent = t;                cmp = cpr.compare(key, t.key);                if (cmp < 0)                    t = t.left;                else if (cmp > 0)                    t = t.right;                else                    return t.setValue(value);            } while (t != null);        }        else {            if (key == null)                throw new NullPointerException();            Comparable<? super K> k = (Comparable<? super K>) key;            do {                parent = t;                cmp = k.compareTo(t.key);                if (cmp < 0)                    t = t.left;                else if (cmp > 0)                    t = t.right;                else                    return t.setValue(value);            } while (t != null);        }        Entry<K,V> e = new Entry<>(key, value, parent);        if (cmp < 0)            parent.left = e;        else            parent.right = e;        fixAfterInsertion(e);        size++;        modCount++;        return null;    }----------
查看完整描述

2 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

泛型是可以定义到方法上,同时如果一个类定义了泛型,那么该类的方法可以使用这个泛型而无需重复定义。
建议你补充你的问题,不是太明白其实

查看完整回答
反对 回复 2019-04-16
  • 2 回答
  • 0 关注
  • 549 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信