Four ways and pros and cons of map traversal in Java

Source: Internet
Author: User

/**
* Use entries in the For-each loop to traverse
* This is the most common and, in most cases, the most desirable way to traverse. Use when key values are required
* If the traversal is an empty map, will be off the group out of Bounds, JAVA5 introduced, incompatible with the old version
* @param map
*/
public void MethodOne (Map<integer, integer> Map) {
For (Map.entry<integer, integer> entry:map.entrySet ()) {
System.out.println ("Key =" + entry.getkey () + ", Value =" + Entry.getvalue ());
}
}
/**
* Traverse keys or values in the For-each loop.
* If you only need the keys or values in the map, you can do the traversal by keyset or by values instead of EntrySet
* 10% higher efficiency than method one
*/
public void Methodtwo (MAP&LT;INTEGER,INTEGER&GT;MAP) {
Traverse key
For (Integer Key:map.keySet ()) {
System.out.println ("key =" + key);
}
Traversing values in a map
For (Integer value:map.values ()) {
System.out.println ("value =" + value);
}
}
/**
* Use iterator traversal, divided into a generic traversal, without a generic traversal
* Advantages compatible with old version, traversal can call remove Delete, For_each traversal cannot delete, traverse speed and for_each like
*/
public void Methodthreey (MAP&LT;INTEGER,INTEGER&GT;MAP) {
Iterator<map.entry<integer, integer>> entries = Map.entryset (). Iterator ();
while (Entries.hasnext ()) {
Map.entry<integer, integer> Entry = Entries.next ();
System.out.println ("Key =" + entry.getkey () + ", Value =" + Entry.getvalue ());
}
}
/**
* No iterator traversal of generic type
* @param map
*/
public void Methodthreen (map map) {
Iterator entries = Map.entryset (). Iterator ();
while (Entries.hasnext ()) {
Map.entry Entry = (map.entry) entries.next ();
Integer key = (integer) entry.getkey ();
Integer value = (integer) entry.getvalue ();
System.out.println ("key =" + key + ", value =" + value ");
}


}
/**
* Through key value traversal (low efficiency)
* Code clean, substitution of method one
*/
public void Methodfour (MAP&LT;INTEGER,INTEGER&GT;MAP) {
For (Integer Key:map.keySet ()) {
Integer value = Map.get (key);
System.out.println ("key =" + key + ", value =" + value ");
}
}

Four ways and pros and cons of map traversal in Java

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.