Map collection of key-value pairs

4. The key-value pairs

4.1 java.util Interface Map <K, V>

  • The key value mapped to the object. A map can not contain duplicate keys; each key can only be mapped to a value at most.
  • Map interface provides three collection views, in order to allow a set of keys or a set of values ​​key - value mapping relationship between form view a set of mapping content.
  • All Known Implementing Classes: HashMap, , . HashtableTreeMap
  • Map Collection and the difference?
    A: Map storage is the key element to form a unique key, value can be repeated.
    B: Collection storage element is independent occurrences, a unique sub-interface element Set, List sub-interface element may be repeated.

Nested Class

  • Map.Entry <K, V> map entries (key - value): public static interface Map.Entry <K, V>
  • Map.Entry <K, V> the Entry <K, V> internal interface Map interface defines the data type for the key-value pair.
  • The HashMap Entry <K, V> implement and extend Map.Entry <K, V>, is defined as the head of the list elements in the array node.
  • The TreeMap Entry <K, V> implement and extend Map.Entry <K, V>, defined as trigeminal list elements, comprising the parent and child pointers.
  • Wherein designed using Static inner class interface internal interface class.

Abstract method

Add Features put

  • V put (K key, V value ): The specified key associated with the specified value in this map, returns the previous value associated with key, if there was no mapping for key, or null.
  • void putAll (Map m <extends K ,? extends V?>): all of the mappings to this map from the map will be specified.

Delete function remove

  • void clear (): Removes the mapping for all mappings.
  • V remove (Object key): If there is a mapping relationship of a key, it is removed from this map, the return value of this map previously associated the key if this map contains no mapping for the key, null is returned.

Judgment function , containsKey containsValue

  • boolean containsKey (Object key): if this map contains a mapping for the specified key, it returns true.
  • boolean containsValue (Object value): If this mapping one or more keys to the specified value, it returns true.
  • boolean equals (Object o): Compares the specified object with this map for equality.
  • boolean isEmpty (): if this map contains no key - value mappings, it returns true.

Acquisition function , , , , entrySet keySet values get size

  • Set <Map.Entry <K, V >> entrySet (): Returns a Set view of the mappings contained in this map, obtain key-value pairs .
  • Set  keySet (): Returns this map contains keys Set view.
  • Collection  values (): Returns this map contains a value Collection view.
  • V get (Object key): Returns the value of the specified key is mapped; if this map contains no mapping for the key, null is returned.
  • int hashCode (): Returns the hash code value for this map.
  • int size (): Returns the key map - value mappings. If the map contains greater than Integer.MAX_VALUE, returns Integer.MAX_VALUE.

Knowledge extension

Map集合的遍历
    A:键找值
        a:获取所有键的集合
        b:遍历键的集合,得到每一个键
        c:根据键到集合中去找值

    B:键值对对象找键和值
        a:获取所有的键值对对象的集合
        b:遍历键值对对象的集合,获取每一个键值对对象
        c:根据键值对对象去获取键和值

    代码体现:
        Map<String,String> hm = new HashMap<String,String>();

        hm.put("it002","hello");
        hm.put("it003","world");
        hm.put("it001","java");

        //方式1 键找值
        Set<String> set = hm.keySet();
        for(String key : set) {
            String value = hm.get(key);
            System.out.println(key+"---"+value);
        }

        //方式2 键值对对象找键和值
        Set<Map.Entry<String,String>> set2 = hm.entrySet();
        for(Map.Entry<String,String> me : set2) {
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key+"---"+value);
        }

4.2 java.util 类 HashMap<K,V>

  • Hash table implementation of the Map interface based, HashSet HashMap is also based on a set of implementation.
  • The underlying data structure is a hash table (array element is a linked list).
  • This class provides the basic operation stability, these basic operations include add, remove, contains and size.
  • This implementation is not synchronized, thread-safe.
  • In addition to allowing the use of non-synchronous and null, HashMap Hashtable class is approximately the same.
  • Examples HashMap has two parameters that affect its performance: initial capacity and load factor .
  • Default load factor (0.75) seeks a compromise in terms of time and space costs. Higher values ​​decrease the space overhead, but also increases the cost of the query.

Specific method (refer to Map)

Nested Class

  • Class AbstractMap.SimpleEntry <K, V> map entries (key - value): public static class AbstractMap.SimpleEntry <K, V>
  • Class AbstractMap.SimpleImmutableEntry <K, V>: maintaining keys and values ​​Entry immutable. This class does not support the setValue method.
  • Have achieved Map.Entry <K, V> which way, but there is no practical significance . Since the HashMap and TreeMap Entry <K, V> is achieved Map.Entry <K, V> internal interface, is not achieved in the internal class AbstractMap.

Construction method

  • public HashMap (): construct a same mappings as the specified Map new HashMap. HashMap initial capacity is created with default load factor (0.75) and sufficient to hold the specified Map mappings.
  • public HashMap (int initialCapacity): configured with the specified initial capacity and a default load factor (0.75) is empty HashMap.
  • public HashMap (int initialCapacity, float loadFactor ): configured with the specified initial capacity and load factor of the air HashMap.
  • public HashMap (Map m <extends K ,? extends V?>): configured mapping relationship with a same specified Map new HashMap.

4.3 java.util 类 TreeMap <K, V>

  • NavigableMap achieve red-black tree (Red-Black tree) based.
  • The natural order of their mapping of sorted keys, or sorted according to a Comparator provided at map creation time, depending on the method of construction used.
  • This implementation provides guaranteed log (n) time overhead containsKey, get, put and remove operations.
  • This implementation is not synchronized, thread-safe.

specific method

Nested class (the same hashMap)

Construction method

  • public TreeMap (): using the natural ordering of a new bond structure, an empty tree map.
  • public TreeMap (Comparator comparator <super K ?>): Constructs a new, empty tree map, according to the given sort comparator.
  • public TreeMap (Map m <extends K ,? extends V?>): Constructs a given map a new tree map having the same mappings, the mappings natural ordering of its keys.

Special methods (mainly using a sorting tree structure of the key)

Returns the key-value pair

  • public Map.Entry <K, V> ceilingEntry (K key): Returns a key - value mapping, it is greater than or equal to the minimum key associated with a given key; If such a key does not exist, null is returned.
  • public Map.Entry <K, V> floorEntry (K key): Returns a key - value mapping, it is less than or equal to the maximum key associated with the given key; If such a key does not exist, null is returned.
  • public Map.Entry <K, V> higherEntry (K key): Returns a key - value mapping, it is associated with the least key strictly greater than the given key; if no such key exists, or null.
  • public Map.Entry <K, V> lowerEntry (K key): Returns a key - value mapping associated with the greatest key strictly less than the given key; If such a key does not exist, null is returned.
  • public Map.Entry <K, V> firstEntry (): returns the associated with this map a minimum bond - value mapping; if the map is empty, null is returned.
  • public Map.Entry <K, V> lastEntry (): Returns the map associated with this key the maximum key - value mapping; if the map is empty, null is returned.
  • public Map.Entry <K, V> pollFirstEntry (): Removes and returns associated with the smallest key map key - value mapping; if the map is empty, null is returned.
  • public Map.Entry <K, V> pollLastEntry (): Removes and returns associated with the greatest key map key - value mapping; if the map is empty, null is returned.

return key

  • public K ceilingKey (K key): returns the smallest key greater than or equal to the given key; If such a key does not exist, null is returned.
  • public K floorKey (K key): Returns the largest key less than or equal to the given key; If such a key does not exist, null is returned.
  • public K higherKey (K key): Returns the key strictly greater than the minimum given key; if no such key exists, null is returned.
  • public K lowerKey (K key): Returns the greatest key strictly less than the given key; If such a key does not exist, null is returned.
  • public K firstKey (): Returns the current map the first (lowest) key.
  • public K lastKey (): Returns the current map last (highest) key.

4.4 java.util 类 Hashtable <K, V>

  • This class implements a hash table, the hash table that maps keys to values.
  • Synchronization, thread-safe.

Special method (the remaining reference to HashMap)

  • Enumeration public  Elements (): Returns the enumeration value of this hash table.
  • Enumeration public  Keys (): Returns an enumeration of the keys in this hash table.
  • protected void rehash (): increase the capacity of the hash table and the restructuring of its inside, to more effectively accommodate and access its elements. When the number of keys in the hash table and the hash table exceeds the capacity of load factor, this method is called automatically.

4.5 java.util Interface Map.Entry <K, V>

  • public static interface Map.Entry <K, V>, internal interface Map interface.
  • It defines the data type of mapping entries (key - value pairs).

Abstract methods , , getKey getValue setValue

  • boolean equals (Object o): Comparison of the specified object with this equality.
  • K getKey (): Returns the key corresponding to this entry.
  • V getValue (): returns the value corresponding to this entry.
  • int hashCode (): Returns the hash code value for this entry map.
  • V setValue (V value): Replace the value corresponding to this specified value.

Knowledge extension

java using external interfaces and the internal interfaces of
a new non-abstract class (general class) that implements the interface as follows:
1. Normal class implements the interface, then all methods must implement the interface
2, in turn, then there is no way to realize interface the class is an abstract class
3, a class can implement multiple interfaces
here with the benefits of inner classes it is: 1, when you only achieve external interface, you can simply implement an interface to an external method (internal interface is not the tone) . Mode 2, to achieve internal interface is "external interface. Internal Interface", then the internal interface method can be achieved. By this method, may be implemented separately from the external interface and internal interfaces , the internal interfaces in the general method of only 3, the internal interface is a data type. 

Reference Links: https://www.jianshu.com/p/ab539e9a7955

4.6 java.util 类 AbstractMap.SimpleEntry<K,V>

  • AbstractMap inner class is realized Map.Entry <K, V>
  • Maintenance Entry keys and values. You can change the value using the setValue method.
  • See specific method Map.Entry <K, V> can.

4.7 comprehensive comparison

  The underlying implementation Whether thread safety
HashMap<K,V> hash table (array element of the list) Unsafe
TreeMap<K,V> Tree (trigeminal list) Unsafe
Hashtable<K,V> hash table (array element of the list) Safety

Guess you like

Origin www.cnblogs.com/Stephanie-boke/p/11515195.html