Value (), keyset () and EntrySet () and performance analysis in the Map collection

Source: Internet
Author: User
Tags set set

In the Map collection

VALUES (): Method is to get all the values in the collection----no key, no correspondence, keyset (): All the keys in the map are saved to the set collection. Because the set has an iterator. All the keys can be iterated out and then based on the Get method. Gets the corresponding value for each key. Keyset (): The key can only be fetched via get () after iteration
EntrySet (): set<map.entry<k,v>> entryset ()//returns the Set view of the mapping relationship contained in this map. Map.entry represents a mapping relationship. EntrySet (): After iteration, you can E.getkey (), E.getvalue () to take key and value. The entry interface is returned.

Here's an example to see:

public class Map {public static void main (string[] args) {map<string, string> maps = new Hashmap<string,stri
		Ng> ();
		Maps.put ("Ten", "AA");
		Maps.put ("One", "BB");
		Maps.put ("A", "CC");
		Maps.put ("", "DD");
		Maps.put ("", "EE");
		 ①value (): Method is to get all the values in the collection----no key, no corresponding relationship collection<string> collections = Maps.values ();
		 System.out.println (collections);//[aa, DD, EE, BB, CC]//②keyset (): Gets all the keys in the collection set<string> sets = Maps.keyset ();
		 SYSTEM.OUT.PRINTLN (sets);//[10, iterator<string> iterators = Sets.iterator ();
			 while (Iterators.hasnext ()) {String key = Iterators.next ();
			 System.out.print ("" +key);//a String val = Maps.get (key); System.out.print ("" +val);//aa DD EE BB CC}//③entryset (): Returns a Set view of the mapping relationships contained in this map. Map.entry represents a mapping relationship//The mapping relationship in the map collection is removed through the EntrySet () method (this relationship is the Map.entry type) set<entry<string, string>> entries = m
		 Aps.entryset (); The relationship set EntrySet is iterated and stored in an iterator iterator<entry<string, string>> Iterator1 = Entries.iterator (); while (Iterator1.hasnext ()) {entry<string, string> Entry = Iterator1.next ();//Get Map.entry Relationship object Entry String key
			 1 = Entry.getkey ();
			 String value1 = Entry.getvalue ();
		 System.out.println (key1+ "--->" +value1);
 } 
	}
}
Description

①set<k> keyset (): The return value is a set set that holds only the key value (unordered storage in the collection) and can only be fetched by a get () key after an iteration.
②set<map.entry<k,v>> EntrySet (): Returns the set set of mapping relationships contained in a map (a relationship is a key-value pair), that is, the (key-value) As a whole, a pair of them are stored in a set set. Iterations can be E.getkey (), E.getvalue () to take key and value. The entry interface is returned.

③ Although using keyset and entryset to traverse can achieve the same result, but the traversal speed of the two is different, keyset () speed than entryset () much slower, that is, keyset way to traverse the map performance is not as good as entryset performance, In order to improve performance, consider using the EntrySet () method for traversal later.

Question: Why is the keyset way to traverse the map less performance than entryset performance.

public class Map1 {public static void main (string[] args) {//Why keyset performance is not as good as entryset.   
	     Experiments show that hashmap<string, string> keysetmap = new hashmap<string, string> (); 
	     hashmap<string, string> entrysetmap = new hashmap<string, string> ();   
	     for (int i = 0; i < 1000000 i++) {keysetmap.put ("" + I, "keyset");   
	     for (int j = 0; J < 1000000; J + +) {Entrysetmap.put ("" + J, "EntrySet");   
	     //keyset experiment: 1 million data, spents long Starttimeone = System.currenttimemillis ();   
	     iterator<string> keysetiterator = Keysetmap.keyset (). iterator ();   
	         while (Keysetiterator.hasnext ()) {String key = Keysetiterator.next ();   
	     String value = Keysetmap.get (key);//performance Gap Reason: when the value corresponding to the key is obtained, this method of accessing the map is System.out.println (value); } System.out.println ("Keyset spent times:" + (System.currenttimemillis ()-StarttiMeone));//54//entryset Experiment: 1 million data, spents long starttimetwo = System.currenttimemillis ();   
	     iterator<entry<string, string>> entrykeyiterator = Entrysetmap. EntrySet (). Iterator ();   
	         while (Entrykeyiterator.hasnext ()) {entry<string, string> e = Entrykeyiterator.next ();   
	     System.out.println (E.getvalue ()); 
	} System.out.println ("EntrySet spent times:" + (System.currenttimemillis ()-starttimetwo));//28  }
 }
Reason Analysis:

By viewing the source code discovery, calling this method Keysetmap.keyset () generates a Keyiterator iterator whose next method returns only its key value.
Java code

Private class Keyiterator extends hashiterator<k> {public   
       K next () {return   
           nextentry (). Getkey ();   
       }   
   }
The call to the Entrysetmap.entryset () method generates a Entryiterator iterator whose next method returns an instance of a entry object that contains key and value.
Java code
Private class Entryiterator extends hashiterator<map.entry<k,v>> {public   
       map.entry<k,v> next () {return   
           nextentry ();   
       }   
Both at this time the performance should be the same, but the way to get the value of the key again and again, this time to access the map of this method, at this time, the way more than one traversal of the table.
In the above case,
String value = Keysetmap.get (key)

See the source can see



This method is the main reason for the difference in performance.
















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.