Map, HashMap

Source: Internet
Author: User

Map (dicing), also known as a Dictionary, is a form set of elements consisting of the Key and its corresponding Element values. Generally, for Map, the given Key can be used to quickly retrieve the corresponding elements from the unit set. Therefore, when you need to search for a large amount of data and the search performance occupies an important position, Map is undoubtedly an ideal container.

 

HashMap: implementation of the Map interface based on the hash table. This implementation provides all the optional ING operations. This class does not guarantee the ing order, especially it does not guarantee that the order remains unchanged.

(1)

The following code is provided:

The above code uses the put method of HashMap to add objects to the ing. The key of the first added object is "a", and its corresponding element value is "zhangsan ".

Because HashMap does not guarantee the order, the order output is not necessarily the same as the order added.

 

(2) What if another object with the same key value is inserted?

Code:

It can be seen that the element values with the same key values will replace the previous element values.

 

(3) How can we reflect the high query performance of HashMap?

Code:

The above code searches for elements whose key values are B and d by using the get () method, so that the element value corresponding to the key value B is lisi. Therefore, the element value is returned.

The object whose key value is d is not in the HashMap, So null is returned.

 

In turn, so that two objects with the same element value and different keys are inserted into HashMap:

It can be seen that values are equal and keys are different, which can also be placed in HashMap.

 

(4)

In Map, there is a keyset () method that returns a Set of keys. Keys in Map cannot be repeated. It can be inferred that the return type of the keySet () method is Set;

There is also a values () method in Map, which returns the value of the element in Map. Because the element values can be repeated, the return type of the values () method is Collection.

 

Therefore, we can use the Set returned by the keySet () method and call its iterator to traverse the elements in the HashMap. Code:

Output result:

A = aa

C = dd

E = ee

B = bb

D = dd

The output sequence is not guaranteed.

 

(5) The second method of traversing HashMap

Use the internal Entry class of Map to obtain the set of keys, and then call the getkey () method and getValue () method of the entry to obtain the key and value in map respectively.

 

 

Map, HashMap

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.