Two methods to retrieve a map set

Source: Internet
Author: User

The map set can be retrieved in two ways,

1. keyset: store keys in the map into the set, and use the set iterator to process all keys.

The sample code is as follows:

Import Java. util. *; Class test {public static void main (string [] ARGs) {Map <string, integer> map = new hashmap <string, integer> (); map. put ("fan", 23); map. put ("peng", 45); map. put ("Cheng", 34); // gets the set <string> keyset = map. keyset (); iterator <string> it = keyset. iterator (); While (it. hasnext () {string keystring = it. next (); system. out. println (keystring + "-" + map. get (keystring ));}}}

2. entryset

The key-value relationship in the Key Map set is returned in the form of a set, and then the set iterator is used

Format: Set <map. Entry <K, V>

The Code is as follows:

Class test {public static void main (string [] ARGs) {Map <string, string> map = new hashmap <string, string> (); map. put ("fan", "fan"); map. put ("peng", "peng"); map. put ("Cheng", "Cheng"); // nested form of generics. The relationship is map. entry <K, V> type set <map. entry <string, string> entryset = map. entryset (); iterator <map. entry <string, string> it = entryset. iterator (); While (it. hasnext () {map. entry <string, string> entry = it. next (); string key = entry. getkey (); string value = entry. getvalue (); system. out. println (Key + "-" + value );}}}

 

Two methods to retrieve a map set

Related Article

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.