Traversal method and performance test _java of map in Java

Source: Internet
Author: User
Tags advantage

1. To elaborate

In the Java map Traversal way, many articles are recommended to use EntrySet, think it is more efficient than keyset. The reason: The EntrySet method gets all the keys and value sets at a time, and keyset gets only a set of keys, which, for each key, goes to the map to find an extra value, which reduces overall efficiency. What about the actual situation?

To understand the real difference in traversal performance, including the differences in different scenarios such as traversing key+value, traversing key, and traversing value, I tried some comparative tests.

2. Comparative test

In the beginning only a simple test, but the result shows that keyset performance better, which I am very puzzling, not all say entryset obviously better than keyset? For further verification, different test data were used for more detailed comparison tests.

2.1 Test Data

2.1.1 HashMap test Data

HashMap-1, size 1 million, key and value are String,key values of 1, 2, 3 ... 1000000:

Copy Code code as follows:

map<string, string> map = new hashmap<string, string> ();
String key, value;
for (i = 1; I <= num; i++) {
Key = "" + I;
Value = "value";
Map.put (key, value);
}

HashMap-2, size 1 million, key and value are string,key values of 50, 100, 150, 、......、 50000000:
Copy Code code as follows:

map<string, string> map = new hashmap<string, string> ();
String key, value;
for (i = 1; I <= num; i++) {
Key = "" + (I * 50);
Value = "value";
Map.put (key, value);
}

2.1.2 TreeMap test Data

TreeMap-1, size 1 million, key and value are String,key values of 1, 2, 3 ... 1000000:

Copy Code code as follows:

map<string, string> map = new treemap<string, string> ();
String key, value;
for (i = 1; I <= num; i++) {
Key = "" + I;
Value = "value";
Map.put (key, value);
}

TreeMap-2, size 1 million, key and value are string,key values of 50, 100, 150, 、......、 50000000, more discrete:
Copy Code code as follows:

map<string, string> map = new treemap<string, string> ();
String key, value;
for (i = 1; I <= num; i++) {
Key = "" + (I * 50);
Value = "value";
Map.put (key, value);
}

2.2 Test Scenarios

The keyset, entryset and values are used to test three scenarios: traversing the key+value, traversing the key, and traversing the value.

2.2.1 Traversal Key+value

Keyset traversal Key+value (notation 1):

Copy Code code as follows:

Iterator<string> iter = Map.keyset (). iterator ();
while (Iter.hasnext ()) {
Key = Iter.next ();
Value = Map.get (key);
}

Keyset traversal Key+value (notation 2):
Copy Code code as follows:

For (String Key:map.keySet ()) {
Value = Map.get (key);
}

EntrySet Traversal Key+value (notation 1):
Copy Code code as follows:

Iterator<entry<string, string>> iter = Map.entryset (). iterator ();
Entry<string, string> Entry;
while (Iter.hasnext ()) {
Entry = Iter.next ();
Key = Entry.getkey ();
Value = Entry.getvalue ();
}

EntrySet Traversal Key+value (notation 2):
Copy Code code as follows:

For (entry<string, string> entry:map.entrySet ()) {
Key = Entry.getkey ();
Value = Entry.getvalue ();
}

2.2.2 Traversal key

Keyset traversal key (notation 1):

Copy Code code as follows:

Iterator<string> iter = Map.keyset (). iterator ();
while (Iter.hasnext ()) {
Key = Iter.next ();
}

Keyset traversal key (notation 2):
Copy Code code as follows:

For (String Key:map.keySet ()) {
}

EntrySet traversal key (notation 1):
Copy Code code as follows:

Iterator<entry<string, string>> iter = Map.entryset (). iterator ();
while (Iter.hasnext ()) {
Key = Iter.next (). Getkey ();
}

EntrySet traversal key (notation 2):
Copy Code code as follows:

For (entry<string, string> entry:map.entrySet ()) {
Key = Entry.getkey ();
}

2.2.3 Traversal Value

Keyset traversal value (notation 1):

Copy Code code as follows:

Iterator<string> iter = Map.keyset (). iterator ();
while (Iter.hasnext ()) {
Value = Map.get (Iter.next ());
}

Keyset traversal value (notation 2):
Copy Code code as follows:

For (String Key:map.keySet ()) {
Value = Map.get (key);
}

EntrySet Traversal value (notation 1):
Copy Code code as follows:

Iterator<entry<string, string>> iter = Map.entryset (). iterator ();
while (Iter.hasnext ()) {
Value = Iter.next (). GetValue ();
}

EntrySet Traversal value (notation 2):
Copy Code code as follows:

For (entry<string, string> entry:map.entrySet ()) {
Value = Entry.getvalue ();
}

Values traverse value (notation 1):
Copy Code code as follows:

Iterator<string> iter = Map.values (). iterator ();
while (Iter.hasnext ()) {
Value = Iter.next ();
}

Values traverse value (notation 2):
Copy Code code as follows:

For (String value:map.values ()) {
}

2.3 Test Results

2.3.1 HashMap Test Results

Unit: MS

HashMap-1

HashMap-2

Keyset traversal Key+value (notation 1)

39

93

Keyset traversal Key+value (notation 2)

38

87

EntrySet Traversal Key+value (notation 1)

43

86

EntrySet Traversal Key+value (notation 2)

43

85

Unit: MS

HashMap-1

HashMap-2

Keyset traversal key (notation 1)

27

65

Keyset traversal key (notation 2)

26

64

EntrySet traversal key (notation 1)

35

75

EntrySet traversal key (notation 2)

34

74

Unit: MS

HashMap-1

HashMap-2

Keyset traversal value (notation 1)

38

87

Keyset traversal value (notation 2)

37

87

EntrySet Traversal value (notation 1)

34

61

EntrySet Traversal value (notation 2)

32

62

Values traverse value (notation 1)

26

48

Values traverse value (notation 2)

26

48

2.3.2 TreeMap Test Results

Unit: MS

TreeMap-1

TreeMap-2

Keyset traversal Key+value (notation 1)

430

451

Keyset traversal Key+value (notation 2)

429

450

EntrySet Traversal Key+value (notation 1)

77

84

EntrySet Traversal Key+value (notation 2)

70

68

Unit: MS

TreeMap-1

TreeMap-2

Keyset traversal key (notation 1)

50

49

Keyset traversal key (notation 2)

49

48

EntrySet traversal key (notation 1)

66

64

EntrySet traversal key (notation 2)

65

63

Unit: MS

TreeMap-1

TreeMap-2

Keyset traversal value (notation 1)

432

448

Keyset traversal value (notation 2)

430

448

EntrySet Traversal value (notation 1)

62

61

EntrySet Traversal value (notation 2)

62

61

Values traverse value (notation 1)

46

46

Values traverse value (notation 2)

45

46

3. Conclusion

3.1 If you use HashMap

1. At the same time traversing key and value, the performance difference between keyset and EntrySet method depends on the specific situation of the key, such as complexity (complex object), divergence, conflict rate, etc. In other words, it depends on the cost of HashMap to find value. EntrySet a one-time removal of all keys and value of the operation is a performance cost, when this loss is less than HashMap to find the cost of value, EntrySet performance advantage will be reflected. For example, when key is the simplest numeric string in the comparison test, keyset may be more efficient and time-consuming than entryset 10% less. Generally speaking, it is recommended to use EntrySet. Because when key is very simple, its performance may be slightly lower than keyset, but it is controllable, and with the complexity of key, EntrySet advantage will be obvious. Of course, we can choose according to the actual situation
2. The keyset method is more suitable when only traversing key, because entryset the useless value also to take out, wasted the performance and the space. In the above test results, keyset is 23% less time-consuming than the EntrySet method.
3. Using the Vlaues method is the best choice when traversing value only, EntrySet is slightly better than the keyset method.
4. In different traversal writing, the recommended use of the following wording, the efficiency is slightly higher:

Copy Code code as follows:

For (String Key:map.keySet ()) {
Value = Map.get (key);
}

For (entry<string, string> entry:map.entrySet ()) {
Key = Entry.getkey ();
Value = Entry.getvalue ();
}
For (String value:map.values ()) {
}

3.2 If you use TreeMap

1. When traversing key and value at the same time, unlike HashMap, entryset performance is much higher than keyset. This is determined by the query efficiency of TREEMAP, that is, the TreeMap to find value is significantly higher than the cost of EntrySet to remove all keys and value at once. Therefore, it is strongly recommended to use the EntrySet method when traversing TreeMap.
2. The keyset method is more suitable when only traversing key, because entryset the useless value also to take out, wasted the performance and the space. In the above test results, keyset is 24% less time-consuming than the EntrySet method.
3. Using the Vlaues method is the best choice when traversing value only, and EntrySet is also significantly superior to the keyset method.
4. In different traversal writing, the recommended use of the following wording, the efficiency is slightly higher:

Copy Code code as follows:

For (String Key:map.keySet ()) {
Value = Map.get (key);
}
For (entry<string, string> entry:map.entrySet ()) {
Key = Entry.getkey ();
Value = Entry.getvalue ();
}
For (String value:map.values ()) {
}

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.