Java map usage

Source: Internet
Author: User

1. Map Traversal

/The most common Traversal method is the most common. Although it is not complex, it is very important. This is what we are most familiar with. I will not say much about it !!
Public static void work (Map <string, student> map ){
Collection <student> C = map. Values ();
Iterator it = C. iterator ();
For (; it. hasnext ();){
System. Out. println (it. Next ());
}
}
// Use keyset for traversal. Its advantage is that it can be worth the values you want based on the key you want, making it more flexible !!
Public static void workbykeyset (Map <string, student> map ){
Set <string> key = map. keyset ();
For (iterator it = key. iterator (); it. hasnext ();){
String S = (string) it. Next ();
System. Out. println (Map. Get (s ));
}
}
// A complicated Traversal method is provided here ~~ He is very violent. He is so flexible that he can get whatever he wants ~~
Public static void workbyentry (Map <string, student> map ){
Set <map. Entry <string, student> set = map. entryset ();
For (iterator <map. Entry <string, student> it = set. iterator (); it. hasnext ();){
Map. Entry <string, student> entry = (Map. Entry <string, student>) it. Next ();
System. Out. println (entry. getkey () + "--->" + entry. getvalue ());
}
}

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.