Selection of--treemap, HashMap key and value traversal of Java map traversal method

Source: Internet
Author: User
Turn from:

http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html 1. Elaborates

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. So what is the 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 said 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:

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:

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);

}

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

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:

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);

}

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):

Iterator<string> iter = Map.keyset (). iterator ();

while (Iter.hasnext ()) {

Key = Iter.next ();

Value = Map.get (key);

}

Keyset traversal Key+value (notation 2):

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

Value = Map.get (key);

}

EntrySet Traversal Key+value (notation 1):

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):

For (entry<string, string> entry:map.entrySet ()) {

Key = Entry.getkey ();

Value = Entry.getvalue ();

}

2.2.2 Traversal keyKeyset traversal key (notation 1):

Iterator<string> iter = Map.keyset (). iterator ();

while (Iter.hasnext ()) {

Key = Iter.next ();

}

Keyset traversal key (notation 2):

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

}

EntrySet traversal key (notation 1):

Iterator<entry<string, string>> iter = Map.entryset (). iterator ();

while (Iter.hasnext ()) {

Key = Iter.next (). Getkey ();

}

EntrySet traversal key (notation 2):

For (entry<string, string> entry:map.entrySet ()) {

Key = Entry.getkey ();

}

2.2.3 Traversal value keyset traversal value (notation 1):

Iterator<string> iter = Map.keyset (). iterator ();

while (Iter.hasnext ()) {

Value = Map.get (Iter.next ());

}

Keyset traversal value (notation 2):

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

Value = Map.get (key);

}

EntrySet Traversal value (notation 1):

Iterator<entry<string, string>> iter = Map.entryset (). iterator ();

while (Iter.hasnext ()) {

Value = Iter.next (). GetValue ();

}

EntrySet Traversal value (notation 2):

For (entry<string, string> entry:map.entrySet ()) {

Value = Entry.getvalue ();

}

Values traverse value (notation 1):

Iterator<string> iter = Map.values (). iterator ();

while (Iter.hasnext ()) {

Value = Iter.next ();

}

Values traverse value (notation 2):

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

" /tr>

Unit: Ms

TreeMap-1

TreeMap-2

Keyset traversal value (notation 1)

432

448

Keyset traversal value (notation 2)

430

448

EntrySet traverse value (notation 1)

62

EntrySet traversing value (writing 2) ""

"

1

Values Traverse value (writing)

46

Values Traverse value (writing 2)

3. Conclusion 3.1 If you use HashMap to traverse key and value at the same time, the performance difference between keyset and EntrySet methods depends on the specifics 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 to only traverse key, keyset method is more appropriate, because the entryset will be useless value also to take out, wasting the performance and space. In the above test results, keyset is 23% less time-consuming than the EntrySet method. Using the Vlaues method is the best choice when traversing value only, EntrySet is slightly better than the keyset method. In different traversal writing, it is recommended to use the following wording, the efficiency is slightly higher:

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 to traverse 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. The keyset method is more appropriate when only the key is traversed, because the entryset takes out the useless value, wasting performance and space. In the above test results, keyset is 24% less time-consuming than the EntrySet method. When traversing value only, using the Vlaues method is the best choice and entryset is significantly better than the keyset method. In different traversal writing, it is recommended to use the following wording, the efficiency is slightly higher:

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 ()) {

}<

Related Article

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.