zoukankan      html  css  js  c++  java
  • Java集合之Map和Set源码分析

    以前就知道Set和Map是java中的两种集合,Set代表集合元素无序、不可重复的集合;Map是代表一种由多个key-value对组成的集合。然后两个集合分别有增删改查的方法。然后就迷迷糊糊地用着。突然在一个风雨交加的夜晚,感觉不能这样迷迷糊糊,得深入地去研究一下,于是去看了看源码(jdk1.8)。

    1.Map源码。

    /**
     * An object that maps keys to values.  A map cannot contain duplicate keys;
     * each key can map to at most one value.
    
     *The Map interface provides three collection view, which
     * allow a map's contents to be viewed as a set of keys, collection of values,
     * or set of key-value mappings.

    这是jdk源码中的对map这个接口的描述,大概意思是说这是一个键值对映射的对象,一个map中不能包含重复的键,每一个键最多映射一个值;map这个接口提供了三个集合视图,一个是关于key的set集合,一个是关于value的collection集合,还有一个是关于key-value映射关系的set集合。分别是以下几个集合对象。

     Set<K> keySet();

     Collection<V> values();

    Set<Map.Entry<K, V>> entrySet();

    可以很明显地看出,map就是set的扩展。看了这个有什么用呢?用途很多,更加深入理解集合,你会被这些设计者(Josh Bloch)的思想所折服—当然这都比较扯淡。来点实际的,以上三种集合的大小都是一样的,因为key-value是一一对应的,所以你有三种方式来遍历map。这位兄台已经进行实验。http://www.2cto.com/kf/201212/179013.html

    public interface Map<K,V> {
        // Query Operations
    ...

    这是jdk1.8中的Map接口的定义,可以发现map并没有继承collection,但是我之前在网上看了好多都说map也继承的collection,让我百思不解。

    2.Set源码。

    public interface Set<E> extends Collection<E> {
        // Query Operations
    ...

    set才是真正地继承了collection接口,map只是在set的基础上的一个扩展。继承collection的还有List;

    /**
     * A collection that contains no duplicate elements.  More formally, sets
     * contain no pair of elements <code>e1</code> and <code>e2</code> such that
     * <code>e1.equals(e2)</code>, and at most one null element.  As implied by
     * its name, this interface models the mathematical <i>set</i> abstraction.

    以上是源码中对Set的描述 set是数学中的集合的概念,正如名字所暗示的一样,java中的Set是对数学中set的抽象,Set中的元素是不能重复的,Set最多可含一个null元素;对于任意的非null元素e1和e2,都满足e1.equals(e2)==false. 并且在Set接口中,还有一些交集和并集的方法,如 addAll(Collection<? extends E> c); containsAll(Collection<?> c);

    (虽然集合号称存储的是Java对象,但实际上并不会真正将Java对象放在集合中,而是在集合中保留对象的引用)

    3.HashMap和hashSet

    (1)HashMap

    HashMap是Map的一个具体实现。HashMap实际上是一个链表散列的数据结构,即数组和链表的结合体。HashMap的底层就是一个数组结构。

     public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
    
        public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }

    以上是是HashMap中的默认的构造方法,设置了DEFAULT_LOAD_FACTOR=0.75f, 还有带参数的构造方法,可以设置负载因子(一种时间和空间成本上的折衷),增大负载因子可以减少所占内存的开销,但是会增加查询数据的时间开销,get()和put()都会用到查询。其他的构造方法中还有一个参数initialCapacity,定义了一个默认的数值DEFAULT_INITIAL_CAPACITY = 1 << 4;结果就是16,一个hashMap初始的容量就是16,但是会动态地改变大小,这里的initialCapacity不等于size()返回的值。

    public V put(K key, V value) {
            return putVal(hash(key), key, value, false, true);
        }
    
        /**
         * Implements Map.put and related methods
         *
         * @param hash hash for key
         * @param key the key
         * @param value the value to put
         * @param onlyIfAbsent if true, don't change existing value
         * @param evict if false, the table is in creation mode.
         * @return previous value, or null if none
         */
        final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
            Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);
            else {
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {
                    for (int binCount = 0; ; ++binCount) {
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();
            afterNodeInsertion(evict);
            return null;
        }

    以上是hashMap中的对于put方法的描述,如果元素重复,则会保留key,替换value;刚刚在介绍Map时提到了Map.Entry这个东西,在hashMap中,Node<K,V>实现了这个接口(static class Node<K,V> implements Map.Entry<K,V> ); 每个key-value都放在了Node<K,V>这个对象中,采用 Node<K,V>[] tab 数组的方式来保存key-value对;HashMap使用一种传说中的“Hash算法”来确定每个元素的存储位置, 调用key的hashCode()方法,通过返回值来确定每个元素的存储位置。如果在数组的该位置上已经存放了其他元素,那么这里的位置将以链表的形式存放。同理,get方法也是如此。

    (2)HashSet以下是HashSet源码中的构造方法:

      /**
         * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
         * default initial capacity (16) and load factor (0.75).
         */
        public HashSet() {
            map = new HashMap<>();
        }

    一看到默认的构造方法就什么都明白了,HashSet是基于HashMap实现的,只是封装了HashMap,源码中也是这样描述的;在HashSet中也说明了initial capacity (16) and load factor (0.75). 初始的容量是16,默认的负载因子是0.75。

    (3)treeMap

    TreeMap中的元素也是存储在一个Entry<K,V>中,但是底层是用一棵“红黑树”来保存Entry,因此,TreeMap添加元素、取出元素的性能比HashMap低。当需要添加元素时,要遍历这棵二叉树才能插入合适的位置,而HashMap是根据hashCode返回值来确定Entry的存放位置,所以TreeMap存取元素比较消耗性能。但正因为如此,TreeMap也有自己的优势,TreeMap中的元素总是保持一种有序的状态。

        public static void main(String[] args) {
            Map map = new TreeMap();
            map.put("9", 9);
            map.put("2", 2);
            map.put("1", 1);
            map.put("4", 4);
    
            Iterator it = map.keySet().iterator();
            while (it.hasNext()) {
                System.out.println(map.get(it.next()));
            }
        }
    //结果是:
    1
    2
    4
    9

     (4)TreeSet

     以下是TreeSet源码中的构造方法,类似于HashSet,封装了一个TreeMap,在TreeSet中元素也是有序的。

    public TreeSet() {
            this(new TreeMap<E,Object>());
        }
  • 相关阅读:
    最大流——poj3308 (模板)
    混合边的欧拉路径——poj1637 最大流
    JBPM FAQ
    spring 读取资源文件方法
    JBPM使用assignHandler进行用户分派思路
    直接修改jpbm xml流程定义字段的方法
    转 java 读取文件的字符集
    JAVA实现AD验证
    找到一篇jbpm session closed解决办法的文章
    dwr 读取cookie
  • 原文地址:https://www.cnblogs.com/hello-daocaoren/p/6764186.html
Copyright © 2011-2022 走看看