Map collection of source code analysis ----

Structure Map implementation class

   Map : double row of data, the data stored in key-value pairs

   HashMap : Map of class as the main achievement; thread-safe, high efficiency; null and can store the key value

   The backing HashMap :
      Array list + -> prior jdk7
      array + + red-black tree list -> jdk8

   A LinkedHashMap : guaranteed while traversing the map element, traversal can be achieved (in order to add the reason: the structure of the original on the bottom of the HashMap, adding a pair of pointers, a front element and a rear pointing to the frequent traversal operation, such the efficiency is higher than HashMap)

   TreeMap : guarantee in accordance with the key-value added of the sort, to achieve the sort traversal. At this time, considering the key or natural ordering custom ordering, using the underlying red-black tree

   Hashtable : As the old implementation class: thread-safe, low efficiency can not store the key and null value

   The Properties : used to handle configuration files. key and value are of type String


Map understanding of the structure:

   Map the key : disorderly, unrepeatable, use the Set stores all of the key -> class key where you want to override equals and hashCode () (to HashMap for example)

   Map of value : disordered, repeatable, all value-> class where the value used to rewrite storage Collection equals ()

   A key-value pair : key-value Entry object constitutes a

   Map of entry : disorderly, unrepeatable, use the Set stores all entry

Two ways to traverse the key-value

   Method 1 :

Set entrySet = map.entrySet();
Iterator iterator1 = entrySet.iterator();
while(iterator1.hasNext()){
      Object obj = iterator1.next();
      //entrySet集合中的元素都是entry
      Map.Entry entry = (Map.Entry) obj;
      //entry.getKey(),entry.getValue()
}

   Second way :

Set entrySet = map.entrySet();
Iterator iterator1 = entrySet.iterator();
while(iterator1.hasNext()){
      Object key = iterator1.next();
      Object value = map.get(key);
}

The realization of the principle underlying HashMap? (Jdk7 for example)
  HashMap map = new HashMap();
  在实例化以后,底层创建了长度是16的一维数组Entry[] table
  ... 可能已经执行过多次put ...
  map.put(key1, value1);

First of all, calling the hashCode class key1 where () key1 calculated hash value, the hash value after after a computational algorithm to obtain the storage location in the Entry array.

   If the data from this position empty, in this case key1-value1 added successfully. -----> 1.
   If the data from this position is not empty, (this position means that there is one or more data (present in the form of a linked list)), and one or more key1 comparing existing data hash value:

         If the hash value with the hash value key1 existing data are not the same, then add key1-value1 success. ----> Case 2
         identical hash values and if a certain data (key2 -value2) already existing key1 hash value, the comparison continues: call the class where key1 equals (key2):

                If equals () returns false: At this time, add key1-value1 successful ----> 3 where
                if equals () Returns true: the use of alternative value1 value2

   2 and 3 on the situation: The case key1-value1 and the original data is stored in a linked list

   Add in the constant process of expansion will involve the issue, when the threshold is exceeded (and to be stored position is not empty), expansion. The default mode of expansion: expansion to the original capacity of 2 times, and copy over the original array

jdk8 compared to jdk7 different in terms of the underlying implementation:

   1, newHashMap (): the bottom layer do not create an array of length 16

   2, jdk8 bottom of the array is: Node [], rather than the Entry []

   3, the first call to put () method to create the underlying array of length 16

   4, jdk7 substructure only: + array list. jdk8 the underlying structure: Array + + black tree list

   When the number of data elements on a certain position in the array index is present as a list> and the current length of the array 8> 64, at which time all the data on the position of the index tree is stored to red-black

DEFAULT_INITIAL_CAPACITY : The default capacity is 16 hashMap

DEFAULT_LOAD_FACTOR : the HashMap default load factor: 0.75

threshold : expansion threshold, = capacity factor * fill: 0.75 * 16 -> 12

TREIFY_THRESHOLD : Bucket in the chain length is greater than the default value, is converted to red-black tree: jdk8

MIN_TREEIFY_CAPACITY minimum hash table capacity of the tub is Node tree: 64


The principle underlying the LinkedHashMap

   Source :

        static class Entry<K,V> extends HashMap.Node<K,V>{
              Entry<K,V> before, after;//能够记录添加的元素先后顺序
              Entry(int hash, K key, V value, Node<K,V> next){
                    super(hash, key, value, next);
              }
        }

Inheritance HashMap, using the keys on the node is Node hashMap Entity he inherited and added two references, respectively before and after. For maintaining a doubly linked list


TreeMap:

    Adding to the TreeMap key-value, must be requested by the object created in the same class
   as to be sorted according to key: (! Not sorted by value) natural order, custom sorting

Natural ordering : Comparable interface implemented, compareTo method override
custom ordering : new Comparator () inner class


Properties: used to handle configuration files, key and value are of type String
Properties pros = new Properties();
FileInputStream fis = new FileInputStream("jdbc.properties");
pros.load(fis);//加载流对应的文件
pros.getProperty("文件中对应的值");
发布了67 篇原创文章 · 获赞 19 · 访问量 9873

Guess you like

Origin blog.csdn.net/qq_41530004/article/details/103923932