HashMap is one of the most frequently-used and important data structures. It provides dictionary operations, and the efficiency of Insert, Search, and Delete operations is high. This article will try to use Java to implement a simplest HashMap. Because of its simplicity, it is easy to see the true design idea of HashMap.What is Hash?
In my understanding, Hash is
is necessary to implement the Containsvalue in map:publicbooleanreturn contains ( value);} There's no difference in performance, so it's misleading to think that contains is a key that determines if there is an associationDefault value size comparisonThe default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 indexHashtable Public Hashtable () {
Analysis of Java HashMap Traversal
In the original memory, Java HashMap traversal is nothing more than for each or iterator. However, the performance, advantages and disadvantages of traversal are general and unknown. For such basic problems, for Wang Er (Java programming fo
[Simple Java] commonly used HashMap method, simplejavahashmap
HashMap is very useful when you need to count elements. For example, count the number of times each character appears in a string:
package simplejava;import java.util.HashMap;import java.util.Map.Entry;public class Q12 { public static void main(String[] args) {
= = null) {if (other.name! = null) return false; } else if (!name.equals (Other.name)) return false; return true; }/* (non-javadoc) * @see java.lang.object#tostring () */@Override public String toString () {Retu RN "Person [name=" + NAme + ", age=" + Age + "]"; }}Why? The output maps appear with the same key.The preliminary judgment is related to the hashcode mechanism of HASHMAP, which detects the hash value of the element key only when th
How does Java sort HashMap by value and javahashmap?
The values of HashMap are unordered and implemented according to the HashCode of the key. How can we sort this unordered HashMap? (TreeMap is similar)
Import java. util. arrayList; import
++];
}
hash = h;
}
return h;
}
hash function
/**
* Applies a supplemental hash function to a given hashcode, which
* Defends against poor quality hash functions. This is critical
* Because HashMap uses power-of-two length hash tables, that
* Otherwise encounter collisions for hashcodes this do not differ
* in lower bits. Note:null keys always maps to hash 0, thus index 0.
*/
tatic int hash (int h) {
//This function en
The implementation of a hash table-based Map interface. This implementation provides all the optional mapping operations and allows null values and null keys to be used. (in addition to not synchronizing and allowing Nulls, theHashMap class is roughly the same as Hashtable .) This class does not guarantee the order of the mappings, especially because it does not guarantee that the order is constant.This implementation assumes that the hash function distributes elements correctly between buckets,
Concept
1. hash
The translation is a hash, and there is also a way to name hash. We should know what we have learned about the data structure. Hash is to use the hash algorithm to obtain a fixed-length output value of any length input value. The hash algorithm is not introduced here.
Java hashmap Implementation 1. Storage Structure
First, we know that the element stored in map is Entry.
.
The
number itself.So the hash () function will only readjust its value if it is greater than or equal to 216 for a non-null hash value.But what good is the adjustment?Let's first look at the put, the hash value is how to deal with (part of the source code):When looking for a bucket, this hash value is the zise-1 with the table above, the initial is 16, we take 16来 example.Think that the algorithm is HashValue Size-1, at this time size-1=15 binary is 1 1 1 1, that is, any like 16 binary 0x?0 (binar
Recently listening to colleagues said that the use of Sparsearray instead of HashMap can improve performance, so the edge of the two classes of data structure for a simple analysis.Data structure of HashMapHashMap is a combination of arrays and linked lists (in the data structure called "chain-Table hashing"), as shown in:Photo Source: Java's HashMap and HashtableData structure of SparsearraySparsearray ref
be found after the linked list, as for why the list is to save space, the list in memory is not continuous storage, So we can use memory more fully.Java's HashMapWhat does that have to do with the hashmap of our topic today? All right, do not square friends, into the subject. We know that the values in HashMap are all key,value, right, in fact, the storage here is very much like the above, the key will be
1 data structure of HASHMAP
HashMap is actually an array of linked lists, the outermost is an array, and the elements of the array are linked lists.
2 HashMap's Put method:
The source code is as follows:
3 source code is as follows HashMap get method:
The source code is as follows:
Analysis of 4 Concurrenthashmap:
Mainly to divide the segment
Resources:
algorithm ideas are as follows:A. Find the corresponding box according to the hashB. Locate the object in the box that you want to delete. Depending on the storage structure of the elements in the box (list or tree), different search methods are used.C. After finding the object to be deleted, the call takes a different node deletion policy depending on the storage structure.The HASHMAP data structure relationship is simple, as follows.KeySet (), valu
In a nutshell: The big difference between the two is that HASHMAP does not guarantee the order of the data put in, while Linkedhashmap guarantees the order of the data put in. In other words, the order in which the data is added HashMap and the order in which the data is traversed is not necessarily, while linkedhashmap guarantees what the data order is when it is added and what the data order is in the
:
Pointers require additional space, so when the node size is small, open addressing method is more space-saving, and if the saving of the pointer space to expand the scale of the hash table, can make the filling factor smaller, which reduces the open addressing method of conflict, thus improving the average search speed
Why is hashmap in java1.8 with red and black trees?After the jdk1.8 version, Jav
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.