Basic java sorting: Map interface and java sorting map interface

Source: Internet
Author: User

Basic java sorting: Map interface and java sorting map interface

To implement key-value ing in Java, you can use the Map interface.

Add and access key-value pairs
Map <Integer, String> keyValues = new HashMap <Integer, String> (); keyValues. put (1, "1"); keyValues. put (2, "22"); keyValues. put (3, "333"); keyValues. put (3, "3, 3"); for (Map. entry <Integer, String> entry: keyValues. entrySet () {System. out. println (entry. getKey () + "" + entry. getValue ());}
1 12 223 3 3 3

When duplicate keys are added, the last added key will overwrite the values corresponding to the same key.

Fill in another Map
Map <Integer, String> keyValues = new HashMap <Integer, String> (); keyValues. put (1, "1"); keyValues. put (2, "22"); keyValues. put (3, "333"); keyValues. put (3, "three"); Map <Integer, String> keyValues2 = new HashMap <Integer, String> (); keyValues. put (4, "4444"); keyValues. put (5, "55555"); keyValues. putAll (keyValues2); for (Map. entry <Integer, String> entry: keyValues. entrySet () {System. out. println (entry. getKey () + "" + entry. getValue ());}
1 12 223 3 3 3 4 44445 55555
Some common methods
System. out. println (keyValues. get (5); // obtain the value of the key boolean isTrue = keyValues. containsKey (3); // whether or not a key int count = keyValues. size (); // number of key-value pairs Set <Integer> keys = keyValues. keySet (); // String output = keyValues. remove (5); // remove a key value. If no key value exists, null keyValues is returned. clear (); // clear all key values boolean isEmpty = keyValues. isEmpty (); // whether the number of key-value pairs is null

 

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.