It is found that hashmap uses the following format to traverse the entire hashmap hashtable in a loop.
Each element in the returned set is of the map. Entry type.
Java code private hashtable <string, string> emails = new hashtable <string, string> ();
- // Method 1: entryset ()
- // Iterator it = emails. entryset (). iterator ();
- // While (it. hasnext ()){
- // Map. Entry M = (Map. Entry) it. Next ();
- // Logger.info ("email-" + M. getkey () + ":" + M. getvalue ());
- //}
- // Method 2: directly recycling
- For (Map. Entry <string, string> M: emails. entryset ()){
- Logger.info ("email-" + M. getkey () + ":" + M. getvalue ());
- }
- // Method 3: Use keyset ()
- Iterator it = emails. keyset (). iterator ();
- While (it. hasnext ()){
- String key;
- Key = (string) it. Next ();
- Logger.info ("email-" + key + ":" + emails. Get (key ));
- }
Private hashtable <string, string> emails = new hashtable <string, string> (); // Method 1: entryset () // iterator it = emails. entryset (). iterator (); // while (it. hasnext () {// map. entry M = (map. entry) it. next (); // logger.info ("email-" + M. getkey () + ":" + M. getvalue (); // Method 2: directly recycle for (map. entry <string, string> M: emails. entryset () {logger.info ("email-" + M. getkey () + ":" + M. getvalue ();} // method 3: Use keyset () iterator it = emails. keyset (). iterator (); While (it. hasnext () {string key; Key = (string) it. next (); logger.info ("email-" + key + ":" + emails. get (key ));}
In addition, we can convert hashmap into a collection and then iterate the output, but the obtained object
Java code
- Map AA = new hashmap ();
- AA. Put ("tmp1", new object (); // append with the same function.
- AA. Remove ("temp1"); // Delete
- For (iterator I = AA. Values (). iterator (); I. hasnext ();){
- Object temp = I. Next ();
- } // Traverse
Map AA = new hashmap (); AA. put ("tmp1", new object (); // replace append with the same function. AA. remove ("temp1"); // Delete For (iterator I = AA. values (). iterator (); I. hasnext ();) {object temp = I. next ();} // traverse