Java-Map source code analysis and features, java-map source code features

Source: Internet
Author: User

Java-Map source code analysis and features, java-map source code features
Come to the end of the paper, and never know how to do this-Lu YouWenqu that clear such, for the source of live water to Zhu Xi

Map architecture:
For example: (1) Map is a ing interface, and the content stored in Map is a key-value Pair (2) AbstractMap is an abstract class inherited from Map, most APIs in Map are implemented. (3) SortedMap is an interface inherited from Map, SortedMap contains key-value pairs for sorting, and the sorting method is through the comparator. (4) NavigableMap inherits from SortedMap. There are a series of navigation methods, such as "getting key-value pairs greater than or equal to an object". (5) TreeMap inherits from AbstractMap and NavigableMap interfaces, therefore, the content in TreeMap is an ordered key-value pair. (6) HashMap inherits from AbstractMap, And the content is also a key-value pair, but the order is not guaranteed. (7) WeakHashMap inherits from AbstractMap, which has different key types than HashMap. WeakHashMap is a weak key. (8) HashTable inherits direary ary and implements Map, so it is a key-value pair, but it does not guarantee the order and is thread safe. Summary: HashMap is a hash list implemented based on the "Zipper method". It is generally used for a single thread and the key value can be empty. It supports Iterator (Iterator) traversing Hashtable is a hash list implemented based on the zipper method. It is thread-safe and can be used in multi-threaded programs. Supports Iterator traversal and Enumeration traversal. WeakHashMap is also a hash list based on the "Zipper method", and the weak key TreeMap is an ordered hash list, which is implemented by the red/black tree. The key values cannot be empty.
Source code of the Map interface of Java 8:


Public interface Map <K, V> {int size (); // Number of boolean isEmpty (); // determines whether it is null boolean containsKey (Object key ); // determine whether a key boolean containsValue (Object value) exists; // determine whether a value V get (Object key) exists; // obtain value V put (K key, V value); // insert a key-value Pair V remove (Object key); // Delete void putAll (Map <? Extends K ,? Extends V> m); // insert a Map into void clear (); // clear the Set <K> keySet (); // returns the Collection of key Sets <V> values (); // returns the value Set <Map. entry <K, V> entrySet (); // object set. Map changes will affect its interface Entry <K, V> {K getKey (); // obtain key V getValue (); // obtain value V setValue (V value); // set the value boolean equals (Object o ); // determine whether the object is equal to int hashCode (); // return hashCode // comparator, compare two keys public static <K extends Comparable <? Super K>, V> Comparator <Map. entry <K, V> comparingByKey () {return (Comparator <Map. entry <K, V >>& Serializable) (c1, c2)-> c1.getKey (). compareTo (c2.getKey () ;}// compare two values: public static <K, V extends Comparable <? Super V> Comparator <Map. entry <K, V> comparingByValue () {return (Comparator <Map. entry <K, V >>& Serializable) (c1, c2)-> c1.getValue (). compareTo (c2.getValue ();} // compare two keys public static <K, V> Comparator <Map. entry <K, V> comparingByKey (Comparator <? Super K> cmp) {Objects. requireNonNull (cmp); return (Comparator <Map. entry <K, V >>& Serializable) (c1, c2)-> cmp. compare (c1.getKey (), c2.getKey ();} // compare two values: public static <K, V> Comparator <Map. entry <K, V> comparingByValue (Comparator <? Super V> cmp) {Objects. requireNonNull (cmp); return (Comparator <Map. entry <K, V >>& Serializable) (c1, c2)-> cmp. compare (c1.getValue (), c2.getValue () ;}// compare whether map is equal boolean equals (Object o); int hashCode (); // hashCode default V getOrDefault (Object key, V defaultValue) {V v; return (v = get (key ))! = Null) | containsKey (key ))? V: defaultValue;} default void forEach (BiConsumer <? Super K ,? Super V> action) {Objects. requireNonNull (action); for (Map. entry <K, V> entry: entrySet () {K k; V v; try {k = entry. getKey (); v = entry. getValue ();} catch (IllegalStateException ise) {// this usually means the entry is no longer in the map. throw new ConcurrentModificationException (ise);} action. accept (k, v) ;}} default void replaceAll (BiFunction <? Super K ,? Super V ,? Extends V> function) {Objects. requireNonNull (function); for (Map. entry <K, V> entry: entrySet () {K k; V v; try {k = entry. getKey (); v = entry. getValue ();} catch (IllegalStateException ise) {// this usually means the entry is no longer in the map. throw new ConcurrentModificationException (ise);} // ise thrown from function is not a cme. v = function. apply (k, v); try {entry. setValue (v);} cat Ch (IllegalStateException ise) {// this usually means the entry is no longer in the map. throw new ConcurrentModificationException (ise) ;}} default V putIfAbsent (K key, V value) {V v = get (key); if (v = null) {v = put (key, value) ;}return v ;}// Delete the default boolean remove (Object key, Object value) Object corresponding to a key and value) {Object curValue = get (key); if (! Objects. equals (curValue, value) | (curValue = null &&! ContainsKey (key) {return false;} remove (key); return true;} // replace the values of a key and oldValue with newValue default boolean replace (K key, V oldValue, V newValue) {Object curValue = get (key); if (! Objects. equals (curValue, oldValue) | (curValue = null &&! ContainsKey (key) {return false;} put (key, newValue); return true;} // replace the key value default V replace (K key, V value) {V curValue; if (curValue = get (key ))! = Null) | containsKey (key) {curValue = put (key, value);} return curValue;} default V computeIfAbsent (K key, Function <? Super K ,? Extends V> mappingFunction) {Objects. requireNonNull (mappingFunction); V v; if (v = get (key) = null) {V newValue; if (newValue = mappingFunction. apply (key ))! = Null) {put (key, newValue); return newValue ;}} return v ;}default V computeIfPresent (K key, BiFunction <? Super K ,? Super V ,? Extends V> remappingFunction) {Objects. requireNonNull (remappingFunction); V oldValue; if (oldValue = get (key ))! = Null) {V newValue = remappingFunction. apply (key, oldValue); if (newValue! = Null) {put (key, newValue); return newValue;} else {remove (key); return null ;}} else {return null ;}} default V compute (K key, BiFunction <? Super K ,? Super V ,? Extends V> remappingFunction) {Objects. requireNonNull (remappingFunction); V oldValue = get (key); V newValue = remappingFunction. apply (key, oldValue); if (newValue = null) {// delete mapping if (oldValue! = Null | containsKey (key) {// something to remove (key); return null;} else {// nothing to do. leave things as they were. return null ;}} else {// add or replace old mapping put (key, newValue); return newValue ;}} default V merge (K key, V value, BiFunction <? Super V ,? Super V ,? Extends V> remappingFunction) {Objects. requireNonNull (remappingFunction); Objects. requireNonNull (value); V oldValue = get (key); V newValue = (oldValue = null )? Value: remappingFunction. apply (oldValue, value); if (newValue = null) {remove (key) ;}else {put (key, newValue);} return newValue ;}}


Copyright Disclaimer: knowledge is shared, and technology is for communication. Leave a blogger's link for reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.