In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use JDK's HashMap". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use JDK's HashMap.
First, let's analyze HashMap.
Static final int DEFAULT_INITIAL_CAPACITY = 16
Maximum capacity 2 to the power of 15 + 1
Static final int MAXIMUM_CAPACITY = 1 MAXIMUM_CAPACITY)
InitialCapacity = MAXIMUM_CAPACITY
/ / initialize the loading factor
If (loadFactor 20) ^ (h > 12)
Return h ^ (h > 7) ^ (h > 4)
}
Static int indexFor (int h, int length) {
Return h & (length-1)
}
Let's first look at the ways to increase it:
Public V put (K key, V value) {
/ / determine whether the key value is empty
If (key = = null)
Return putForNullKey (value)
/ / get hashcode
Int hash = hash (key.hashCode ())
/ / get a subscript that is less than the length of the array (and operation)
Int I = indexFor (hash, table.length)
/ / find the entry value of the subscript in the array
/ / in fact, entry is also a linked list, and its entry is one-way compared to LinkedList.
For (Entry e = table [I]; e! = null; e = e.next) {
Object k
/ / if there is an entry whose key value is the same object, overwrite the old value with the new value. If it doesn't exist, look down until the end.
If (e.hash = = hash & & ((k = e.key) = = key | | key.equals (k) {
V oldValue = e.value
E.value = value
E.recordAccess (this)
Return oldValue
}
}
/ / increase the number of modifications
ModCount++
/ / add this entry to the head of the subscript list
AddEntry (hash, key, value, I)
Return null
}
Void addEntry (int hash, K key, V value, int bucketIndex) {
Entry e = table [bucketIndex]
/ / visible is the first place on the subscript list
Table [bucketIndex] = new Entry (hash, key, value, e)
/ / size has been added here
If (size++ > = threshold)
Resize (2 * table.length)
}
If key is null:
Then he does not have to use hashcode to locate the queue subscript in the array-_-| | in fact, he does not have the hashcode; to store the header of the linked list in bit 0; it can be seen in HashMap.
Store null's key; but because it doesn't have hashcode, it can only store one element, not multiple elements like others; but in addition, as we can see, you can consider making
The key of the most frequently used value is set to null because it is the fastest to find
Private V putForNullKey (V value) {
For (Entry e = table [0]; e! = null; e = e.next) {
If (e.key = = null) {
V oldValue = e.value
E.value = value
E.recordAccess (this)
Return oldValue
}
}
ModCount++
AddEntry (0, null, value, 0)
Return null
}
It says above that it is stored, so let's take a look at how to take it out:
Final Entry getEntry (Object key) {
/ / the hashcode corresponding to this key is obtained through the algorithm, which shows that the hashcode is fixed, not random; if it is null, it is 0, and the operation is still 0, directly.
/ / navigate to table [0] or find the subscript
Int hash = (key = = null)? 0: hash (key.hashCode ())
For (Entry e = table [indexFor (hash, table.length)]
E! = null
E = e.next) {
Object k
/ / match the hashcode and the value of this key. It can be seen that if the value is not the same, the hashcode may be the same. Otherwise, if it is an one-to-one correspondence, there is no need to match key.
If (e.hash = = hash & &
((k = e.key) = = key | | (key! = null & & key.equals (k)
Return e
}
/ / not found. Empty
Return null
Let's take a look at how its capacity has grown:
Public void putAll (Map
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.