An empty string can be used as the key of hashmap.

Source: Internet
Author: User

In Java, an empty string can be used as the key of hashmap with the string type as the key, for example:

Public static void main (string [] ARGs) {// todo auto-generated method stubsystem. out. println ("----->"); hashmap <string, string> mfilecache = new hashmap <string, string> (); mfilecache. put ("", "Null String"); mfilecache. put ("ABC", "ABC string"); system. out. println ("[" + mfilecache. get ("") + "]"); system. out. println ("[" + mfilecache. get ("ABC") + "]"); system. out. println ("<-----");}

The output result is as follows:

----->
[Null String]
[ABC string]
<-----

In addition, hashmap also runs the null key and null value. The following describes hashmap in JDK:

Java. util. hashmap <K, V>

Type parameter:

K-The type of the key maintained by this ing
V-Type of the mapped Value

Hash table-basedMapInterface implementation. This implementation provides all the optional ing operations and allows the useNullValue andNullKey. (Except for non-synchronization and allow the use of null,HashmapClass andHashtable.) This class does not guarantee the order of mappings, especially it does not guarantee that the order remains unchanged.

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.