Map. Entry and map. entryset (). Use classes to traverse hashmap and hashtable.

Source: Internet
Author: User

By mistake, we found that hashmap uses the following format to traverse the entire hashmap hashtable in a loop.

Each element in the returned set isMap.EntryType.

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 ));
}

// Method 5: Use keyset () (04.29)

For (Object M: emails. keyset ()){
Logger.info ("email-" + M + ":" + emails. Get (m ));
}

In addition, we can convert hashmap into a collection and then iterate the output, but the obtained object

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

 

To complete the internal sorting of elements containing the treeset

Public static void main (string [] ARGs ){
Arraylist <string> List = new arraylist <string> ();
Hashmap <object, Object> hash = new hashmap <object, Object> ();
Treemap <object, Object> treemap = new treemap <object, Object> ();
List. Add ("");
List. Add ("B ");
List. Add ("C ");

Hash. Put (3, 3 );
Hash. Put (4, 4 );
Hash. Put (5, 5 );
Hash. Put (6, 6 );
Hash. Put (1, 1 );
Hash. Put (2, 2 );

Treemap. Put (1, 1 );
Treemap. Put (2, 2 );
Treemap. Put (3, 3 );
Treemap. Put (4, 4 );
Treemap. Put (5, 5 );
Treemap. Put (6, 6 );

// List Traversal
For (string M: List ){
System. Out. println (m );
}
// Hashmap entryset () Traversal
For (Map. Entry <object, Object> M: hash. entryset ()){
System. Out. println (M. getkey () + "---" + M. getvalue ());
}
// Hashmap keyset () Traversal
For (Object M: hash. keyset ()){
System. Out. println (m + "---" + hash. Get (m ));
}
// Treemap keyset () Traversal
For (Object M: treemap. keyset ()){
System. Out. println (m + "---" + treemap. Get (m ));
}
}

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.