Map<String,Object> map=new HashMap<String,Object>详解

When the code slowly began to knock on the more the more, I began to think about life the ~ (Wulian)

When I Coding   the Map <String, Object> = the Map new new HashMap <String, Object>  , I Qiaoxia is what?

1. the Map : i.e.   Interface the Map <K, V>  K - Key Type  V - the type of value

Map is an interface that is to say, each of which contains a key element of the object and a target value (which may be two different types of objects), and there is a correspondence relationship of mapping between the two objects, so from access Map when the collection element, by specifying the key to find the corresponding value, and therefore must be unique key can not be repeated, when the value of the previous key value is the same, the following values ​​override value;

Since that Map is an interface, then look at some of the common methods defined in Map:

V get(Object key)
Returns the specified key is mapped, or nullthis map does not contain the key mapping.
int hashCode()
Returns the hash code value for this map.
boolean isEmpty()
If this map contains no key - value mappings, it returns to true .
Set<K> keySet()
Returns Setthis view the key included in the mapping.
V put(K key, V value)
The value specified in this map is associated with the specified key (optional).

  There are many classes implement the Map interface such as  TreeMapHashtableSortedMap,HashMap,这里只对HashMap进行介绍;

2. the HashMap : i.e.  Class the HashMap <K, V> , hash table based on the Map interface. This implementation provides all optional map operations, and permits  null values and empty key. This class does not guarantee the order of the map; in particular, it does not guarantee that the order will remain constant over time.

①.HashMap data structure

   In the Java programming language, the basic structure is the two kinds, one is an array, a pointer to another (reference), the HashMap is to be achieved by these two data structures. HashMap is actually a "hash list" data structure, i.e., a combination of arrays and lists. I.e. HashMap underlying structure is an array, each array is a linked list. When a new HashMap and it will initialize an array.

Summarize is: HashMap in the bottom of the key-value process as a whole, this is a whole Entry object. HashMap bottom using a Entry [] array to store all of the key-value pairs, when required to store a Entry object, will decide its storage position in the array according to the hash algorithm, determined under the equals method which in this array position storage location of the list; Entry when a need to remove, will also find its storage position in the array according to the hash algorithm, then remove the Entry from the list according to position on the equals method.

 ② . The HashMap The two parameters affecting its performance: initial capacity and load factor. 

    Capacity is the number of buckets in the hash table, the initial capacity is simply the ability to create time in the hash table. Load factor is a measure of the spatial extent of use of a hash table, the greater the load factor, the higher the degree of filling of the hash table, vice versa smaller. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table will be re-hash (i.e., reconstruction of the internal data structures), so that a hash table with about twice the number of barrels. As a general rule, the default load factor (0.75) between the time and space cost of providing a good trade-off. Higher values decrease the space overhead but increase the lookup cost (reflected in the HashMap most operating class, including get and PUT ). When setting the initial capacity, the expected number of entries in the mapping and loading factors should be considered, in order to minimize the number of operations to re-hash. If the initial capacity greater than the maximum number of entries divided by the load factor does not occur reload operation.

③. Since HashMap implements Map interface, it must implement all methods in all interfaces, so do not write it here -

The following is a simple example:

 

        Map <String, Object> map = new HashMap <String, Object> (); // Create Object Map all types of objects parent 
        map.put ( "publish", publish) ; // store key and value 
        map.put ( "Status", Status); 
as map.get ( "publish"); // get the value of the corresponding key

 

You can also refer to this article to learn more about the realization of the principle HashMap: http://wiki.jikexueyuan.com/project/java-collection/hashmap.html

About Map and HashMap we can also look at the official website (direct click), I hope this article can help you.

 

Guess you like

Origin www.cnblogs.com/wyhhh/p/11286178.html