Java Map traversal mode performance analysis: Ketset and EntrySet

Source: Internet
Author: User

KeySet ():deposits all the keys in the map into the set collection. Because set has an iterator, you can take all the keys iteratively, and then get the value for each key based on the Get method, which only takes the key through get (). EntrySet (): returns a Set view of the mappings contained in this map, formatted as SET<MAP.ENTRY<K,V>>, Map.entry represents a mapping relationship, and can be E.getkey (), E.getvalue () after iteration Takes the key and value, and returns the entry interface. KeySet () mode
 set  <String> keySet = Map.keyset () ; //first gets the set set of all keys for the map collection 
< span> iterator<string> it = keyset.iterator () ; //has a set set so that it can get its iterator
while (It.hasnext ()) {
String key = It.next () ;
String value = Map.get (key) ; //has a key to get its corresponding value through the Get method of the map collection.
}
EntrySet () mode:
//Use the EntrySet () method to remove the mapping in the map collection (this relationship is the Map.entry type)
Set<map.entry<string, string>> EntrySet = Map.entryset ();
//EntrySet The relationship collection to the iterator
Iterator<map.entry<string, String>> it2 = Entryset.iterator ();
while(It2.hasnext ()) {
Map.entry<string, string> me = It2.next ();//Get Map.entry Relationship Object me
String Key2 = Me.getkey ();//Get key through a relational object
String value2 = Me.getvalue ();Get value from a relational object
}
Performance test:
Public static voidMain(string[] args) {
Hashmap<string, String> Keysetmap =NewHashmap<string, String> ();
Hashmap<string, String> Entrysetmap =NewHashmap<string, String> ();
for(inti =0; I <100000; i++) {
Keysetmap.put ("" N, "KeySet");
Entrysetmap.put ("" N, "EntrySet");
}
LongStarttimeone = System.Currenttimemillis();
iterator<string> keysetiterator = Keysetmap.keyset (). Iterator ();
while(Keysetiterator.hasnext ()) {
String key = Keysetiterator.next ();
String value = Keysetmap.get (key);
System. out. println (Key +"," + value);
}
System. out. println ("Keyset spent times:"+ (System.Currenttimemillis()-Starttimeone));

LongStarttimetwo = System.Currenttimemillis();
Iterator<map.entry<string, string>> entrykeyiterator = Entrysetmap.entryset (). Iterator ();
while(Entrykeyiterator.hasnext ()) {
Map.entry<string, String> e = Entrykeyiterator.next ();
System. out. println (E.getkey () +"," + E.getvalue ());
}
System. out. println ("EntrySet spent times:"+ (System.Currenttimemillis()-Starttimetwo));
}
The result of the operation is as follows, KeySet () is much slower than EntrySet (). Cause analysis: When using keyset mode, the performance is consistent after commenting out Keysetmap.get (key). As shown, that is to say, the keyset way to get value and then re-traverse the map collection, loss of performance. It is therefore not recommended to use keyset for value.



From for notes (Wiz)



Java Map traversal mode performance analysis: Ketset and EntrySet

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.