Java hashmap analysis 4: Search and memory usage

Source: Internet
Author: User
Document directory
  • Get Element
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?

 
  1. Public v get (Object key ){
  2. If (Key = NULL)
  3. Return getfornullkey ();
  4. Int hash = hash (key. hashcode ());
  5. For (Entry <K, V> E = table [indexfor (hash, table. Length)];
  6. E! = NULL;
  7. E = E. Next ){
  8. Object K;
  9. If (E. Hash = hash & (k = E. Key) = Key | key. Equals (k )))
  10. Return e. value;
  11. }
  12. Return NULL;
  13. }

 

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?

 
  1. Public static void main (string [] ARGs ){
  2. // Todo code application logic here
  3. Hashmap <string, string> map = new hashmap <string, string> ();
  4. String put = map. Put ("A", "B ");
  5. String size = sizeof. humanreadable (sizeof. deepsizeof (MAP ));
  6. System. Out. println (size );
  7. }

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.

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.