Map - and the king of Chu Xiangshi

Foreword

  Life, we often see such a collection: IP address and host name, ID number and individuals, this relationship corresponding eleven, called mapping . Java provides special collection class objects used to store the correspondence relationship, i.e. java.util.Map interface.

  Collection under the set set of interface in the Map interface, different forms of data are stored, a single set of interface definition Collection specification, each time a memory element; and set the Map interface specification is set double row, each storing a the elements.

  Collection in collection of elements exist in isolation ( single dog ), by way of a storage element to the collection storage elements.

  Map of the collection element is present in pairs ( top dog ), each element of the key and the value of two parts, the corresponding values can be found through the key.

  Collection single collection is called a set, set the Map called dual column set. Note that, the set of the Map can not contain duplicate keys, values ​​can be repeated, and each key corresponds to only one value.

Map popular sub-category

  Map multiple sub-categories, here mainly about HashMap collection, LinkedHashMap collection.

  • The HashMap : storing the data structure is used in the hash table, the access order of the elements can not be guaranteed consistent. Due to guarantee uniqueness, not duplicate key, hashCode () method, equals () method needs to be rewritten keys.
  • LinkedHashMap : LinkedHashMap under the HashMap with a subclass, the stored data using a hash table structure + list structure. Linked list structure may ensure consistent access order of the elements; by hash table structure can be guaranteed to be unique, not a duplicate key, the key needs to be rewritten hashCode () method, the equals () methods.

  The Tip : Map collection interface are two generic variables, in use, to impart two generic data type variables. Two generic data types of variables may be the same or different.

Map interface commonly used methods:

  Map interface defines a number of methods commonly used are:

  • put (K key, V value) and added value of the specified key to the Map collection.
  • remove (Object key) the designated key corresponding to the key to delete the set of elements from the Map, the return value of the element is deleted.
  • get (Object key), acquires a value corresponding to a set of sheets in the Map with a specified key.
  • Set <K> keySet () Get Map set all the keys stored in the Set collection.
  • Set <Map.Entry <K, V >> entrySet () to get a collection Map collection of key-value pairs for all objects (Set collection).

  The Tip : Use put () method when, if the specified key (key) is not in the collection, the key is not a value corresponding return null, and add to the collection specified key value;

  If the specified key (key) is present in the set, the key corresponding to the value (the value before replacement value) is set, and the value corresponding to the specified key, replaced with a new value for the specified is returned.

Map collection traversal key way to find value

  Get the value of the key, i.e. the key elements by obtaining the value corresponding to the key.

  Analysis steps:
  1. Map obtain all the keys, because the key is unique, so the key to return to the set to store all of a Set. Methods Tip: keyset ().
  2. Traversal keys Set collection to get each key.
  3. The key to obtaining a value corresponding to the key. Methods Tip: get (K key)

Entry key object

   We already know, the Map is stored in two kinds of objects, called a key (key), called value (value), which is one relationship in the Map, this object is also called to do Map a entry (entry) in. Entry package corresponding relation to the keys become objects. That key, and we, can obtain the key value corresponding to a key-value from each corresponding pair (the Entry) object while traversing the object Map collection.

 Since Entry shows a pair of keys and values, it also provides a corresponding key and the corresponding acquisition worth method:

  • public K getKey (): Get Entry object key.
  • public V getValue (): Get Value Entry object.

 In the Map collection also provides an overview of all Entry object:

  • public Set <Map.Entry <K, V >> entrySet (): Gets to the collection Map collection of key-value pairs for all objects (Set collection).

Map collection of key-value pairs traverse way

  Key on the way: namely (the Entry) object, retrieving key-value pairs (the Entry) object keys and key values ​​for each set.

  The procedure is illustrated:
  1. Get Map in the collection, all key-value pairs (Entry) object, and returns to Set aggregate form. Methods Tip: entrySet ().
  2. Traversal key-value pair comprising Set (Entry) collection of objects, each to give a key-value pair (the Entry) object.
  3. Of (Entry) object, retrieving keys and values ​​Entry object by key value. Methods Tip: getkey (), getValue ().

 

  Tip : the Map sets can not be used directly or foreeach iterator traversal, but then turn into Set is ready for use.

LInkedHashMap

  We know that the only element pairs HashMap guarantee, and the query is fast, but is not paired elements stored into the order, then we have to ensure the orderly, but also how fast to do it?

  In the following a HashMap subclass LinkedHashMap, it is a data storage structure list and combined hash table.
public class LinkedHashMapDemo {
    public static void main(String[] args) {
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("邓超", "孙俪");
        map.put("李晨", "范冰冰");
        map.put("刘德华", "朱丽倩");
        Set<Entry<String, String>> entrySet = map.entrySet();
        for (Entry<String, String> entry : entrySet) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}

  result:

Deng Chao Sun Li 
Li Chen Fan Bingbing 
Andy Lau, Michelle Yeoh

 

Guess you like

Origin www.cnblogs.com/qiuhaitang/p/12010815.html
map
Map
Map
map