About HashMap

HashMap principle of analytic

Yeah HashMap with for a long time, but has been only use it, simply do not understand what there is to how to achieve ... Recently finally understand a little bit.

 

1. First of all we are speaking about the fundamental use of HashMap: data storage and access of data

>>> Create Map
the HashMap <String, String> = new new the HashMap Map <String, String> ();

>>> storing data
map.put ( "name", "zhangsan");
map.put ( "Age", " 18 is ");
map.put (" Sex "," MALE ");

>>> 1 by way of a data acquisition keyset traversing
the Iterator Iterator = map.keySet () Iterator ();.
the while (iterator.hasNext ()) {
  String = Key (String) Iterator.next ();
  String value = as map.get (Key);
  System.out.println ( "traversal KeySets: Key =" + Key + ", value =" + value);
}

>>> Get 2 by way of data traversing entrySet
the Iterator entryIterator = EnumMap.entrySet () Iterator ();.
the while (entryIterator.hasNext ()) {
  the entry entry = (the entry) entryIterator.next ();
  String key = (String) entry.getKey();
  String value = (String) entry.getValue();
  System.out.println("EntrySet遍历:key="+key+",value="+value);
}

Compare *** traversal of keyset and entryset

keySet : () method Key map acquired by the next, and then obtain a value corresponding to the value (which is set to the Set stored only map set key) as map.get by a (key)

the entrySet : () obtained by Method Next Entry object, can be obtained respectively the corresponding values entry.getKey (), entry.getValue () (stored inside is set to Set Entry object, comprising a set key, value)

In summary : entrySet than keySet more efficient way to traverse some recommended ways 2 entrySet traverse the map collection.

 

Use HashMap Code Example: HashMapApp.java  

package com.study.thread.juc_thread.base;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMapApp {

	public static void main(String[] args) {
		//创建map
		HashMap<String,String> map = new HashMap<String,String>();
		
		//存储数据
		map.put("name","zhangsan");
		map.put("age", "18");
		map.put("sex", "male");
		
		//获取数据方式1  通过keyset遍历
		Iterator iterator = map.keySet().iterator();
		while(iterator.hasNext()){
			String key = (String) iterator.next();
			String value = map.get(key);
			System.out.println("KeySet遍历:key="+key+",value="+value);
		}
		
		System.out.println ( "--------------- ------------------ traversal parting line"); 
		
		// Get 2 by way of data traversing entrySet 
		the Iterator entryIterator = EnumMap.entrySet () Iterator ();. 
		the while (entryIterator.hasNext ()) { 
			the entry entry = (the entry) entryIterator.next (); 
			String Key = (String) entry.getKey ( ); 
			String value = (String) entry.getValue (); 
			System.out.println ( "EntrySet traverse: Key =" + Key + ", value =" + value); 
		} 
				

	} 

}

  

 2. Next we look at HashMap initialization, the object is to create a map

  The default capacity is: 16   

  The default load factor: 0.75

    Capacity: i.e., the initial size of the map set, then the expansion will be performed according to the value of the load factor and the number of objects stored

  Load factor: i.e., when a map data set storage capacity of more than 0.75, the expansion operation may occur

       For example: a capacity of 16, a load factor of 0.75, when the stored data is larger than 12, hash conflict occurs, and the array of each map are already under each bucket values, expansion occurs (the original size becomes set 2 times)

  *** Recommended specified capacity created map, because the expansion will be very resource-consuming. The initial capacity value = (the object to be stored / load factor) +1

  

Providing four HashMap initialization method, the following

    /**
       1.指定初始化容量,负载因子
     */
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

    / ** 
     specified initial capacity value 
     * / 
    public the HashMap (int initialCapacity) { 
        the this (initialCapacity, DEFAULT_LOAD_FACTOR); 
    } 

    / ** 
      no-argument constructor 
     * / 
    public the HashMap () { 
        this.loadFactor = DEFAULT_LOAD_FACTOR; // All Fields OTHER Defaulted 
    } 

    / ** 
      specified Map 
     * / 
    public the HashMap (the Map m <K the extends, the extends V??>) { 
        this.loadFactor = DEFAULT_LOAD_FACTOR; 
        putMapEntries (m, to false); 
    }

  

3. Then we look at HashMap how to store data, where we need to understand the data structure of the HashMap (hash how to resolve the conflict).

  + Array list, red-black tree hashcode method ....

  pending upgrade....

4. Finally, we look at how to get HashMap data. 

Guess you like

Origin www.cnblogs.com/DFX339/p/11233814.html