In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
JavaHashMap three kinds of loop traversal way and its performance comparison example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The following describes the three loop traversal modes of Java HashMap and their performance comparison. The details are as follows:
Three ways of traversing HashMap
(1) for each map.entrySet ()
Map map = new HashMap (); for (Entry entry: map.entrySet ()) {entry.getKey (); entry.getValue ();}
(2) display the collection iterator that calls map.entrySet ()
Iterator iterator = map.entrySet () .iterator (); while (iterator.hasNext ()) {entry.getKey (); entry.getValue ();}
(3) for each map.keySet (), and then call get to get
Map map = new HashMap (); for (String key: map.keySet ()) {map.get (key);}
Performance Test and comparison of three traversal modes
Test environment: Windows7 32-bit system 3.2G dual core CPU 4G memory, Java 7 eclipse-Xms512m-Xmx512m
Test results:
Map size 10000 100000 1000000 2000000 for each entrySet 2ms 6ms 36ms 91ms for iterator entrySet 0ms 4ms 35ms 89ms for each keySet 1ms 6ms 48ms 126ms
Result analysis of ergodic mode
As can be seen from the above table:
For each entrySet is equivalent to for iterator entrySet performance for each keySet is more time-consuming to call get (key) to obtain the value (it will be more time-consuming if the hash hashing algorithm is poor). If you want to delete map during the loop, you can only use for iterator entrySet (described in HahsMap non-thread safety).
HashMap entrySet source code
Private final class EntryIterator extends HashIterator {
Public Map.Entry next () {
Return nextEntry ()
}
}
HashMap keySet source code
Private final class KeyIterator extends HashIterator {public K next () {return nextEntry () .getKey ();}
As can be seen from the source code:
Both keySet () and entrySet () are iterators that return set. The parent class is the same, but the return value is different, so the performance is similar. It's just that keySet () takes one more step according to the operation of key get value. The time complexity of get depends on the number of for loops, that is, hash algorithm.
Public V get (Object key) {
If (key = = null) return getForNullKey ()
Entry entry = getEntry (key)
Return null = = entry? Null: entry.getValue ();}
/ * *
1. Returns the entry associated with the specified key in the
2. HashMap. Returns null if the HashMap contains no mapping 3. For the key. * / final Entry getEntry (Object key) {
Int hash = (key = = null)? 0: hash (key)
For (Entry e = table [indexFor (hash, table.length)]
E! = null
E = e.next) {
Object k
If (e.hash = = hash & & (k = e.key) = = key | | (key! = null & & key.equals (k) return e
} return null
}
Key and value are required in the loop, but map is not deleted. Key and value are required in the for each entrySet loop, and map is deleted in the for iterator entrySet loop. Only key is needed in the for iterator entrySet loop, and for each keySet is used.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.