Java Basics--about the traversal keyset and entryset__ traversal of the map

Source: Internet
Author: User

The first is a small example of traversal:

public static void Main (string[] args) {
        //TODO auto-generated method Stub
        map<string, string> maps = new H Ashmap<string, string> ();
        Maps.put ("A", "111a");
        Maps.put ("222", "222b");
        Maps.put ("333", "333c");
        Maps.put ("444", "444d");
        Maps.put ("555", "555e");
        Maps.put ("666", "666f");
        
        For (String Str:maps.keySet ()) {
            System.out.println (str + ":" + maps.get (str));
        }
        
        System.out.println ("--------------");
        
        For (entry<string, string> str:maps.entrySet ()) {
            System.out.println (str + "   " + str.getkey () + ":" + str.) GetValue ());
        }

    


As for the performance of the two:

The test found that the performance of the second approach is usually one-fold higher than the first method.

Examples are as follows:

Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;

Import Java.util.Map.Entry; /** * Test keyset () and EntrySet () Iteration time * Keyset (): After iteration, the key * EntrySet () can only be taken via get (): After iteration, E.getkey (), E.getvalue () take key and value. Returns the entry interface * The final description of the keyset () is slower than the EntrySet () a lot faster. It seems to be considered later with EntrySet () * @author YL * @date 2009.6.10/public class Hashmaptest {public static void main (string[] Ar
		GS) {hashmap<string,string> kmap = new hashmap<string,string> ();
		
		hashmap<string, string> emap = new hashmap<string, string> ();
		Load data for (int i = 0; i < 1000 i++) {kmap.put ("" +i, "YL");
		for (int i = 0; i < 1000 i++) {emap.put ("" +i, "ZT");
		Long stimes = System.currenttimemillis ();
		Long ctimes = Calendar.getinstance (). Gettimeinmillis ();
		
		Long dtimes = new Date (). GetTime ();
		
		Initial time here I used three kinds of value methods to find System.currenttimemillis (), the most direct method of System.out.println (stimes+ "" +ctimes+ "" +dtimes); iterator&Lt
		string> ktor = Kmap.keyset (). iterator ();
		while (Ktor.hasnext ()) {System.out.println (Ktor.next ());
		Long stimes1 = System.currenttimemillis ();
		Long ctimes1 = Calendar.getinstance (). Gettimeinmillis ();
		
		Long dtimes1 = new Date (). GetTime ();
		End world and also entryset start Time System.out.println ((stimes1-stimes) + "" + (Ctimes1-ctimes) + "" + (Dtimes1-dtimes));
		
		System.out.println (stimes1+ "" +ctimes1+ "" +dtimes1);
		iterator<entry<string, string>> itor = Emap.entryset (). iterator ();
			while (Itor.hasnext ()) {entry<string, string> e = Itor.next ();
			System.out.println (E.getkey ());
		System.out.println (E.getvalue ());
		Long stimes2 = System.currenttimemillis ();
		Long ctimes2 = Calendar.getinstance (). Gettimeinmillis ();
		Long dtimes2 = new Date (). GetTime ();
		System.out.println (stimes2+ "" +ctimes2+ "" +dtimes2);
	System.out.println ((stimes2-stimes1) + "" + (CTIMES2-CTIMES1) + "" + (dtimes2-dtimes1));
 }
}


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.