Keyset () and entryset ()

Source: Internet
Author: User
  1. ImportJava. util. calendar;
  2. ImportJava. util. date;
  3. ImportJava. util. hashmap;
  4. ImportJava. util. iterator;
  5. ImportJava. util. Map. entry;
  6. /**
  7. * Test the iteration time of keyset () and entryset ().
  8. * Keyset (): The key can only be obtained through get () after iteration.
  9. * Entryset (): After iteration, you can use E. getkey () and E. getvalue () to get the key and value. The entry interface is returned.
  10. * The keyset () is much slower than entryset. It seems that we will consider using entryset () later.
  11. * @ Author YL
  12. * @ Date 2009.6.10
  13. */
  14. Public ClassHashmaptest
  15. {
  16. Public Static VoidMain (string [] ARGs)
  17. {
  18. Hashmap <string, string> kmap =NewHashmap <string, string> ();
  19. Hashmap <string, string> emap =NewHashmap <string, string> ();
  20. // Load data
  21. For(IntI = 0; I <1000; I ++)
  22. {
  23. Kmap. Put ("" + I, "yl ");
  24. }
  25. For(IntI = 0; I <1000; I ++)
  26. {
  27. Emap. Put ("" + I, "ZT ");
  28. }
  29. LongStimes = system. currenttimemillis ();
  30. LongCtimes = calendar. getinstance (). gettimeinmillis ();
  31. LongDtimes =NewDate (). gettime ();
  32. // In the initial time, I used three value methods and finally found that system. currenttimemillis (); is the most direct value method.
  33. System. Out. println (stimes + "" + ctimes + "" + dtimes );
  34. Iterator <string> ktor = kmap. keyset (). iterator ();
  35. While(Ktor. hasnext ())
  36. {
  37. System. Out. println (ktor. Next ());
  38. }
  39. LongStimes1 = system. currenttimemillis ();
  40. LongCtimes1 = calendar. getinstance (). gettimeinmillis ();
  41. LongDtimes1 =NewDate (). gettime ();
  42. // End the world and entryset Start Time
  43. System. Out. println (stimes1-stimes) + "" + (ctimes1-ctimes) + "+ (dtimes1-dtimes ));
  44. System. Out. println (stimes1 + "" + ctimes1 + "" + dtimes1 );
  45. Iterator <entry <string, string> itor = emap. entryset (). iterator ();
  46. While(Itor. hasnext ())
  47. {
  48. Entry <string, string> E = itor. Next ();
  49. // System. Out. println (E. getkey ());
  50. System. Out. println (E. getvalue ());
  51. }
  52. LongStimes2 = system. currenttimemillis ();
  53. LongCtimes2 = calendar. getinstance (). gettimeinmillis ();
  54. LongDtimes2 =NewDate (). gettime ();
  55. System. Out. println (stimes2 + "" + ctimes2 + "" + dtimes2 );
  56. System. Out. println (stimes2-stimes1) + "" + (ctimes2-ctimes1) + "+ (dtimes2-dtimes1 ));
  57. }
  58. }

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.