java 淺介map之遍歷用法

    hashMap和hashTable的區別(都是實現Map的接口)
    一、特色(hashMap容許key和value爲空,但key具備惟一性,速率快,非線程安全、hashTable,線程安全、速率慢--加鎖等待耗時間)
    二、簡單介紹keySet()、values()、forEach()、entrySet()進行遍歷及源碼

    public class Test22
{
    public static void main(String[] args)
    {
        //Map的特色是以key-value的形式進行存儲
        Map<String, Object> data = new HashMap<>();
        for (int i = 1; i <= 10; i++ )
        {
            data.put("" + i, i);
        }
        // 獲取keys集合
        Iterator<String> it = data.keySet().iterator();
        while (it.hasNext())
        {
            String key = it.next();
            System.out.print(key + data.get(key) + "  ");
        }
        System.out.println();
        // 獲取values集合
        Iterator<Object> map = data.values().iterator();
        while (map.hasNext())
        {
            Object value = map.next();
            System.out.print(value + "  ");
        }
        System.out.println();
        // 獲取entry遍歷
        for (Map.Entry<String, Object> entry : data.entrySet())
        {
            System.out.print(entry.getKey() + entry.getValue() + "  ");
        }
        System.out.println();
        // Map自帶的forEach方法遍歷
        data.forEach((k, v) -> {
            System.out.print(k + v + " ");
        });
    }安全

}this

運行結果:
11  22  33  44  55  66  77  88  99  1010  
1  2  3  4  5  6  7  8  9  10  
11  22  33  44  55  66  77  88  99  1010  
11 22 33 44 55 66 77 88 99 1010 線程

//keySet()源碼
/*    public Set<K> keySet() {
        Set<K> ks = keySet;
        if (ks == null) {
            ks = new KeySet();
            keySet = ks;
        }
        return ks;
    }
    
    final class KeySet extends AbstractSet<K> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<K> iterator()     { return new KeyIterator(); }
        public final boolean contains(Object o) { return containsKey(o); }
        public final boolean remove(Object key) {
            return removeNode(hash(key), key, null, false, true) != null;
        }
        public final Spliterator<K> spliterator() {
            return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super K> 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.key);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }
    
    final class KeyIterator extends HashIterator
        implements Iterator<K> {
        public final K next() { return nextNode().key; }
    }
*/接口

//values()源碼
/*     public Collection<V> values() {
        Collection<V> vs = values;
        if (vs == null) {
            vs = new Values();
            values = vs;
        }
        return 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();
            }
        }
    }
    
    final class ValueIterator extends HashIterator
        implements Iterator<V> {
        public final V next() { return nextNode().value; }
    }
*/rem

//entrySet()源碼
/*  public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
    }get

    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<Map.Entry<K,V>> iterator() {
            return new EntryIterator();
        }
        public final boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
            Object key = e.getKey();
            Node<K,V> candidate = getNode(hash(key), key);
            return candidate != null && candidate.equals(e);
        }
    
    final class EntryIterator extends HashIterator
        implements Iterator<Map.Entry<K,V>> {
        public final Map.Entry<K,V> next() { return nextNode(); }
    }
*/源碼

//forEach()源碼
/*    public void forEach(BiConsumer<? super K, ? 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.key, e.value);
            }
            if (modCount != mc)
                throw new ConcurrentModificationException();
        }
    }
*/hash

相關文章
相關標籤/搜索