Comparison of traversal methods of HashMap

Table of Contents Comparison of traversal methods of HashMap 1, Several ways to get value from HashMap traversal Two, code Three, result Four, thinki...
1, Several ways to get value from HashMap traversal
Two, code
Three, result
Four, thinking

Table of Contents

Comparison of traversal methods of HashMap

1, Several ways to get value from HashMap traversal

Two, code

Three, result

Four, thinking

Comparison of traversal methods of HashMap

1, Several ways to get value from HashMap traversal

1. First, get the keyset, traverse the key in the keyset, and get the value through the key;

2. Get map.values() and traverse to get the value value;

3. Get the entrySet() and get the value (or key) by traversing the entry.

Two, code

public class MainActivity extends Activity { HashMap<String, String> hashmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hashmap = new java.util.HashMap(); for (int i = 0; i < 1000000; i++) { hashmap.put(""+i, "thanks"); } } public void test(View view) { //The first way is to traverse the key and value of HashMap by traversing Map.keySet() firstMethod(hashmap); //The second way: traverse the value of Map by traversing values(), but not the key secondMethod1(hashmap); secondMethod2(hashmap); //The third way: use iterator() to traverse the key and value of HashMap through Map.entrySet() thirdMethod1(hashmap); thirdMethod2(hashmap); } private static void firstMethod(Map<String, String> map) { long startTime = System.currentTimeMillis(); for (String key : map.keySet()) { } Log.v("testMap","Type 1 time consuming:"+(System.currentTimeMillis() - startTime)); long startTime2 = System.currentTimeMillis(); Iterator iterator2 = map.keySet().iterator(); while (iterator2.hasNext()) { Object key2 = iterator2.next(); } Log.v("testMap","Type 1 2 time consuming:"+(System.currentTimeMillis() - startTime2)); } private static void secondMethod1(Map<String, String> map) { long startTime = System.currentTimeMillis(); Collection<String> values = map.values(); for(Iterator<String> it2 = values.iterator();it2.hasNext();){ it2.next(); } Log.v("testMap","Second 1 time consuming:"+(System.currentTimeMillis() - startTime)); } private static void secondMethod2(Map<String, String> map) { long startTime = System.currentTimeMillis(); for (String v : map.values()) { } Log.v("testMap","Second 2 time consuming:"+(System.currentTimeMillis() - startTime)); } private static void thirdMethod1(Map<String, String> map) { long startTime = System.currentTimeMillis(); Iterator<Map.Entry<String, String>> it3 = map.entrySet().iterator(); while(it3.hasNext()){ Map.Entry<String, String> entry = it3.next(); // entry.getKey(); entry.getValue(); } Log.v("testMap","Type 3 1 time consuming:"+(System.currentTimeMillis() - startTime)); } private static void thirdMethod2(Map<String, String> map) { long startTime = System.currentTimeMillis(); //Use this traversal method when map capacity is large for (Map.Entry<String, String> entry : map.entrySet()) { // entry.getKey(); entry.getValue(); } Log.v("testMap","Type 3 2 time consuming:"+(System.currentTimeMillis() - startTime)); }

Three, result

Print results

07-23 12:36:52.334 3680-3680/com.rrjc.testhashmap V/testMap: the first type 1 time consumption: 538

07-23 12:36:52.662 3680-3680/com.rrjc.testhashmap V/testMap: type 1 2 time consuming: 328

07-23 12:36:53.012 3680-3680/com.rrjc.testhashmap V/testMap: type 2 1 time consuming: 350

07-23 12:36:53.362 3680-3680/com.rrjc.testhashmap V/testMap: second type 2 time consuming: 350

07-23 12:36:53.745 3680-3680/com.rrjc.testhashmap V/testMap: the third 1 time consumption: 383

07-23 12:36:54.129 3680-3680/com.rrjc.testhashmap V/testMap: the third type 2 time consuming: 384

Four, thinking

1. From the first way, it can be seen that it takes a long time for map to get key, and it takes a longer time to get value through the key;

The second and third methods are almost time-consuming.

2. Many people often argue about whether it is faster through iterator mode or foreach mode? But the experiment has already given the conclusion, these two kinds are similar.

3. But at the same time, even though the amount of data has been so large, their iterative efficiency is not significantly different.

4. Finally, there are small partners who like to study. Why is it so time-consuming for map to get key?

31 January 2020, 20:43 | Views: 2684

Add new comment

For adding a comment, please log in
or create account

0 comments