Document directory
Get Element
With the previous analysis, the logic for retrieving elements is very clear. First, the caller passes the key and obtains the value from the hashcode method of the key. Then, the caller calls the hash function to perform some low-level replacement to ensure the even distribution of the hash value, and size-1 to get the position of the array after the bitwise AND. Then, retrieve the linked list at the corresponding position, traverse the linked list, find the objects with the same hash value and the same reference or value of the key, and then return. The code can be found below:
[Java]View plaincopyprint?
- Public v get (Object key ){
- If (Key = NULL)
- Return getfornullkey ();
- Int hash = hash (key. hashcode ());
- For (Entry <K, V> E = table [indexfor (hash, table. Length)];
- E! = NULL;
- E = E. Next ){
- Object K;
- If (E. Hash = hash & (k = E. Key) = Key | key. Equals (k )))
- Return e. value;
- }
- Return NULL;
- }
The average time complexity of the algorithm is O (1). If the hash code is bad, It is O (n ). even O (1), you should note that it takes several steps to calculate the hash, which is definitely longer than getting an element from an array.
Memory consumption
There is a good tool that can help us check the memory consumption of Java objects. Download the jar package from here: http://sizeof.sourceforge.net/
Decompress the package and select sizeof. jar is copied to a directory, such as my/home/chenshu. Add this jar package to the project and set the JVM parameter-javaagent:/home/chenshu/sizeof. jar.
This class library provides some static functions that use the instrumentation. getobjectsize () of Java. Lang. instrument to calculate the memory size occupied by Java objects in virtual machines. The following code creates a hashmap that only saves one object and calculates the memory usage.
[Java]View plaincopyprint?
- Public static void main (string [] ARGs ){
- // Todo code application logic here
- Hashmap <string, string> map = new hashmap <string, string> ();
- String put = map. Put ("A", "B ");
- String size = sizeof. humanreadable (sizeof. deepsizeof (MAP ));
- System. Out. println (size );
- }
The result is 304 bytes, 64 bitjvm. It is really a waste of memory. It is much larger than I estimate! It can be seen that hashmap is not used to store a small amount of data. In addition, considering the complexity of hash calculation, if you only like the key-value interface like map, but do not store a large amount of data, you should consider other maps. Java actually provides many types of map. The result of misuse of hashmap is that it can only develop "enterprise-level" applications, and the old programmers like me laugh at the rich generation. :)
Therefore, hashmap is a good choice when you need to quickly search for and insert a large amount of data (I think more than 10 thousand. However, if the data size is small, the map implemented by tree is also a good choice. After all, it saves a lot of memory. In addition, tree can also implement data structures such as set, which sometimes better meet our needs than map.
If you use hashmap without thinking about it now (I know there are too many programmers), please be careful. What makes you ordinary is not a tight project schedule or low salary, but a lack of high requirements on yourself.