(1) set --- traverse the map set

Source: Internet
Author: User
Tags map class

Map Interface
The class that implements the map interface is used to store key-value pairs.
Implementation classes of the map interface include hashmap and treemap.
Key-value pairs stored in the map class are identified by keys. Therefore, key-value pairs cannot be repeated.

Hashmap: The thread is unsafe and efficient. The key or value is allowed to be null.
Hashtable: thread security and low efficiency. The key or value is not allowed to be null.
Properties: subclass of hashtable. Both key and value are strings.
Common Methods:
 
Object put (Object key, object value );
Object get (Object key );
Object remove (Object key );
Boolean containskey (Object key );
Boolean containsvalue (object value );
Int size ();
Boolean isempty ();
Void putall (MAP t );
Void clear ();

Note: Put usage: When an element is added, when the key value is the same, the added value overwrites the corresponding value of the original key.
And put back the value corresponding to the original key

The first method to traverse the map set

     //  The first method of proportional hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put (  "  A  " , "  Shang Xiaofei  "  ); Maps. Put (  "  B " , "  Shang xiaoshuai  "  ); Maps. Put (  "  C  " , "  Shang Xiaoyan  "  );  //  Obtains a set of all keys and traverses the Set set to retrieve the value of map. Set <string> Set =Maps. keyset ();  For (Iterator = Set  . Iterator (); iterator. hasnext ();) {string key = (String) iterator. Next (); system.  Out . Println ( "  Test. Main ()  " + "  Key -->  " + Key + "  Value --> " + Maps. Get  (Key ));} 
View code

Method 2: traverse the map set

 

 //  Method 2 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put (  "  A  " , " Shang Xiaofei  "  ); Maps. Put (  "  B  " , "  Shang xiaoshuai  "  ); Maps. Put (  "  C  " , "  Shang Xiaoyan  "  ); Maps. Put ( "  A  " , "  Sdafasd  "  );  //  Obtains a set of all keys and traverses the Set set to retrieve the value of map. Set <string> Set = Maps. keyset ();  For (String: Set  ) {System.  Out . Println ( "  Test. Main ()  " + Maps. Get  ());} 
View code

 

The third method to traverse the map set

 

     //  Method 3 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put (  " A  " , "  Shang Xiaofei  "  ); Maps. Put (  "  B  " , "  Shang xiaoshuai  "  ); Maps. Put (  "  C  " , " Shang Xiaoyan  "  ); Maps. Put (  "  A  " , "  Sdafasd  "  ); Set <Entry <string, string> entryset = Maps. entryset ();  For (Iterator = Entryset. iterator (); iterator. hasnext ();) {entry <String, string> entry = (Entry <string, string>) Iterator. Next (); system.  Out . Println ( "  Test. Main ()  " + Entry. getkey () + "  ----  " + Entry. getvalue ());} 
View code

 

Method 4: traverse Map sets

 

     //  Method 4 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put (  "  A  " , "  Shang Xiaofei  "  ); Maps. Put (  "  B  " , "  Shang xiaoshuai  " ); Maps. Put (  "  C  " , "  Shang Xiaoyan  "  ); Maps. Put (  "  A  " , "  Sdafasd  "  ); Set <String> Set =Maps. keyset (); iterator <String> iterator = Set  . Iterator ();  While  (Iterator. hasnext () {string key = Iterator. Next (); system.  Out . Println ( "  Test. Main ()  " + Maps. Get  (Key ));}} 
View code

 

 

 

 

 

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.