Containers--map and Abstractmap

Source: Internet
Author: User

First, preface

Before we introduced the definition of collection interface and a series of implementation, and focused on the list system of some implementation, for collection, in fact, there are set series is also very important, but because set is dependent on the map implementation, so we first introduce map.

Collection is characterized by the storage of a set of elements, while map describes a set of mapping relationships, which we often call key-value structures. This type of container is characterized by a key value pair for each item in the container, the value of the key cannot be duplicated, but the existence of a null key and a null value is allowed, a key can only correspond to one value, but multiple different keys can correspond to the same value.

Because the uniqueness of the key value, so its key set is naturally a set, so the JDK set implementation is based on the idea, using the key set in the map to achieve.

Ii. Main methods

Although the stored elements are different, but the same as the collection, map and collection still have a lot of features similar or even the same function, the following table compares the following:

comparison of methods for map and collection interfaces
Method description Map Method Name Collection Method Name
Number of elements in the container Size () Size ()
Determine if the container is empty IsEmpty () IsEmpty ()
adding elements Put (K key, V value) Add (e E)
Adding elements in bulk

Putall (map<k, v> Map)

AddAll (collection<e> Collection)
Delete Element Remove (Object key)

Remove (Object obj)

RemoveAll (collection<e> Collection)

Retainall (collection<e> Collection)

Clear List Clear () Clear ()
Determine if an element exists

ContainsKey (Object key)

Containsvalue (Object value)

Contains (Object e)

Containsall (collection<e> E)

Traverse

KeySet ()

EntrySet ()

VALUES ()

Iterator ()
Comparison Equals (Object obj) Equals (Object o)
Seek hash Hashcode () Hashcode ()
Find Value by key V get (Object key) No corresponding method
Convert to arrays No corresponding method

ToArray ()

ToArray (t[] T)

As we can see from the table above, both containers provide the ability to add, modify, delete, and traverse the container, and for map, because its unique mapping structure provides the ability to find value based on key, while collection is a set of elements of the same type, So it provides a way to convert to ToArray.

Three, the realization principle of abstractmap

Abstractmap, as the abstract implementation class of MAP, provides the implementation of most methods. The only one that is not implemented is the EntrySet () method, which returns a set with an element type of map.entry.

1) since set belongs to collection, we can know that the most functions of collection and map are similar by the above table, so the realization of these functions can be realized according to the corresponding method of EntrySet. For example, for a delete method, the JDK implements this:

1  PublicV Remove (Object key) {2iterator<entry<k,v>> i =EntrySet (). iterator ();3Entry<k,v> Correctentry =NULL;4         if(key==NULL) {5              while(correctentry==NULL&&I.hasnext ()) {6Entry<k,v> e =I.next ();7                 if(E.getkey () = =NULL)8Correctentry =e;9             }Ten}Else { One              while(correctentry==NULL&&I.hasnext ()) { AEntry<k,v> e =I.next (); -                 if(Key.equals (E.getkey ())) -Correctentry =e; the             } -         } -  -V OldValue =NULL; +         if(Correctentry! =NULL) { -OldValue =Correctentry.getvalue (); + I.remove (); A         } at         returnOldValue; -}

2) does not support the implementation of the Put method, the corresponding, Putall method because the call put, so this method is not supported.

3) by definition, both the key and the value in the map may be null, so determining whether a map contains a key should not be judged by the get (key) = = null and should be called ContainsKey (key).

4) KeySet () and values () are implemented using anonymous inner classes, and the main implementation logic is to rewrite the iterator method of the abstract container class and turn its traversal into a traversal of key and value.

Iii. Summary

Abstractmap through the definition of EntrySet as an abstract method, the implementation of the specific implementation and storage logic to the sub-class, so that the implementation can focus on the storage of elements, in addition, by learning the method in the Abstractmap, we can learn from its map of the traversal mode , the right thing to do is to find EntrySet first, just like this:

1  Public BooleanContainsKey (Object key) {2         iterator<map.entry<k,v>> i = entryset (). Iterator (); 3         if(key==NULL) {4              while(I.hasnext ()) {5Entry<k,v> e =I.next ();6                 if(E.getkey () = =NULL)7                     return true;8             }9}Else {Ten              while(I.hasnext ()) { OneEntry<k,v> e =I.next (); A                 if(Key.equals (E.getkey ())) -                     return true; -             } the         } -         return false; -}

When traversing, we use iterator () instead of using the for (String Key:map.keySet ()) method. Because the problem with this approach is that if the abstract class is implemented, Map.keyset () is equivalent to traversing the entire map, and in the For Loop, for Map.get (key), it actually needs to traverse the entire map, how many times it iterates, This is really very inefficient.

Of course, perhaps HashMap does not traverse the entire map at Get (key), but this does not guarantee that the other implementations are not, so iterator is Nyaya.

will continue to study hashmap tomorrow.

Containers--map and Abstractmap

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.