java core technology 10 reading (nine) - Mapping

Mapping used to store key / value pairs.
The basic mapping operation
HashMap and TreeMap, HashMap key for hashing, TreeMap sort elements integral with the ordering of the keys. Hash slightly faster, if no access keys in the order, it is best to choose a hash.
Update map entry
counts.merge (word, 1, Integer :: sum);
will be associated with word 1, otherwise Integer :: sum function combines the original value and 1 (1 is the original value of the sum).
View map
view (View) mapping: keyset set value (not a set) and a key / value pairs set, they are objects that implement Collection interface or a sub-interface.
Set <Map.Entry <K, V >> entrySet ()
Returns Map.Entry objects (map of key / value pairs) of a set of view. This element can be removed from the centralized, delete them from the map, but you can not add any elements.
Weak hash map
objects garbage collector to track activity. As long as the map object is active, all of the bucket is active, they can not be recovered.
When the only reference to the key from the hash bar, WeakHashMap will work with the garbage collector with the delete key / value pairs.
Link hash sets and maps
LinkedHashSet and LinkedHashMap class to remember the order of the elements inserted item.
Links hash map will be used to access the order, not the order of insertion of map entries iterate. Each call to get or
put, the affected entry will be deleted from the current location, and entry into the tail of the list (only affected entry position in the list, and the hash table bucket will not be affected. The total is located in an entry with key loose column number corresponding to the bucket).
LinkedHashMap <K, V> (initialCapacity , loadFactor, true)
access order to achieve cache "least recently used" principle is very important. For example, you may want to visit a high frequency elements in memory, while less frequently accessed elements from the database to read. . When Element not found entry in the table and the table has been filled, the iterator can be added to the table, and delete the enumeration of the first few elements. These are the least recently used several elements.
Enumeration set mapping
EmimSet type element is set to achieve an efficient enumeration. Since only limited instances enumerated type, the
internal EnumSet implemented with bit sequence. If the corresponding value in the set, the corresponding bit is set to 1.
View
keySet Set method returns a class object that implements the interface, the original methods of this class mapping operation. This set is called a view.
View has some limitations that may only be read, not to change the size, only supports deleted without support the insertion of these views with the key in a mapping. If the attempted improper handling, restricted view will throw an UnsupportedOperationException.

Published 15 original articles · won praise 1 · views 127

Guess you like

Origin blog.csdn.net/qq_17236715/article/details/105059600