Several different traversal, iterative methods and comparison __java of map in Java

Source: Internet
Author: User

In Java map According to the different data structure, there are many different implementations, common such as hash HashMap, linked list linkedmap, hash list linkedhashmap, tree table (binary tree) TreeMap.

In this paper, we discuss several different traversal methods of the HASHMAP hash list which are most commonly used in our programming, and compare the writing and efficiency among them.


First, prepare a map hash table, storing 100,000 records.

Key is from Key0 start to key99999

Value starts from Hello0 until hello99999

public static void Main (string[] args) {
    	map<string,object> map=new hashmap<string,object> ();
     	for (int i=0;i<100000;i++) {
     		map.put ("Key" +i, "Hello" +i);
     	}
     
	


Traversal mode one: EntrySet () + Enhanced for loop

   Map traversal mode one   //2425.7
      static void Iterator_type1 (Map<string,object> map) {
    	
    	  set<map.entry< String, object>> set= map.entryset ();
    	  Long Start=new Date (). GetTime ();
     	  For (map.entry<string, object> item:set) {
     		      String key=item.getkey ();
     		      Object Value=item.getvalue ();
     		  	System.out.println ("Key:" +key+ "  " + "value:" +value);
     	  }
    	  Long End=new Date (). GetTime ();
    	 SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) + "millisecond");
    	  
     }

Traversal mode two: EntrySet () +iterator Iteration

MAP  Traversal mode two  2408.9
      static void Iterator_type2 (Map<string,object> map) {
    	  set<map.entry< String, object>> set= map.entryset ();
    	  Iterator<map.entry<string, object>>  it=  set.iterator ();
    	  Long Start=new Date (). GetTime ();
       	while (It.hasnext ()) {
      		map.entry<string, object> item=   it.next ();
      		String Key=item.getkey ();
      		Object Value=item.getvalue ();
      		System.out.println ("Key:" +key+ "  " + "value:" +value);
      	}
    	  Long End=new Date (). GetTime ();
     	 SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) + "millisecond");
      }

Traversal mode three: keyset () + iterator iteration


Map  Traversal mode three  2441.0
      static void Iterator_type3 (Map<string,object> map) {
    	  set<string> keys=   Map.keyset ();
    	  Iterator<string> it=	keys.iterator ();
    	  Long Start=new Date (). GetTime ();
	      	 while (It.hasnext ()) {
	      		   String key=it.next ();
	      		 Object Value=map.get (key);
	      		   System.out.println ("Key:" +key+ "  " + "value:" +value);
	      	 }
    	  Long End=new Date (). GetTime ();
    	  SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) + "millisecond");
      }

Traversal mode four: keyset () + Enhanced for loop


      MAP  Traversal mode four   2445.5
      static void Iterator_type4 (Map<string,object> map) {
    	  set<string> keys=   Map.keyset ();
    	  Long Start=new Date (). GetTime ();
    	  for (String Key:keys) {
    		  Object value=map.get (key);
      		 System.out.println ("Key:" +key+ "  " + "value:" +value);
      		}
    	  Long End=new Date (). GetTime ();
    	  SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) + "millisecond");
      }
      



Each of these methods is called 10 times in the main function, and the time-consuming data for each traversal is as follows.

public static void Main (string[] args) {
    	map<string,object> map=new hashmap<string,object> ();
     	for (int i=0;i<100000;i++) {
     		map.put ("Key" +i, "Hello" +i);
     	}
     	Iterator_type1 (map);//2425.7
     	//iterator_type2 (map);//2408.9
     	//Iterator_type3 (map);//2441.0
     	// Iterator_type4 (map);//2445.5
	}

Unit millisecond Traversal mode one: 2385 2374 2357 2371 2424 2490 2528 2371 2490 2467 Average time consuming 2425.7

Traversal mode two: 2385 2503 2386 2334 2401 2406 2394 2414 2428 2438 Average time consuming 2408.9

Traversal mode three: 2391 2401 2481 2471 2423 2436 2481 2449 2414 2463 Average time consuming 2441.0

Traversal mode four: 2471 2392 2545 2365 2463 2465 2487 2428 2414 2425 Average time consuming 2445.5


It can be seen from the result data of the test that in the above four kinds of traversal methods. Adopting EntrySet method is more efficient than keyset mode, and the iterator in EntrySet is more efficient than that of enhancing for.

The most efficient way to traverse is the Entryset+iterator way, followed by entryset+ enhanced for, again keyset+iterator, most times keyset+ enhanced for


----The above conclusion is only for reference, the conclusion of the pure personal test is no practical theoretical basis.



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.