第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Collection接口沒(méi)有toString方法,那這個(gè)輸出調(diào)用的到底是誰(shuí)的toString方法呢?

Collection接口沒(méi)有toString方法,那這個(gè)輸出調(diào)用的到底是誰(shuí)的toString方法呢?

開(kāi)心每一天1111 2019-03-01 10:38:46
問(wèn)題簡(jiǎn)述 我去查了下API,發(fā)現(xiàn): Collection接口里沒(méi)有toString方法 map.values()返回值是個(gè)Collection集合 那么問(wèn)題來(lái)了,這個(gè)c1調(diào)用的究竟是誰(shuí)的toString方法? 萌新求助,大神留步,么么噠 Collection<Integer> c1 = map.values(); System.out.println(c1); 源代碼 import java.util.Set; public class Demo044 { public static void main(String[] args){ //demo01(); //demo02(); Map<String,Integer> map = new HashMap<>(); map.put("z3",23); map.put("z4",24); map.put("z5",25); map.put("z6",26); Collection<Integer> c1 = map.values(); System.out.println(c1); } }
查看完整描述

1 回答

?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先一切類(lèi)都是Object類(lèi)的子類(lèi)

你查看HashMap的源碼,values方法,返回的是一個(gè)內(nèi)部類(lèi)Values對(duì)象

    public Collection<V> values() {
        Collection<V> vs;
        return (vs = values) == null ? (values = new Values()) : vs;
    }

    final class Values extends AbstractCollection<V> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<V> iterator()     { return new ValueIterator(); }
        public final boolean contains(Object o) { return containsValue(o); }
        public final Spliterator<V> spliterator() {
            return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super V> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (int i = 0; i < tab.length; ++i) {
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
                        action.accept(e.value);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }

這個(gè)內(nèi)部類(lèi)沒(méi)有覆蓋toString()方法,所以找它的父類(lèi)AbstractCollection

public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }

所以用的是AbstractCollection的toString方法

查看完整回答
反對(duì) 回復(fù) 2019-03-01
  • 1 回答
  • 0 關(guān)注
  • 682 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)