Data structure of HashMap in Java

Source: Internet
Author: User

class declaration:

Overview:
    • Thread is not secure;
    • <key, value> both can be null;
    • The order of the mappings is not guaranteed, especially if it does not guarantee that the order is constant;
    • HashMap use iterator;
    • The default size of the hash array in HashMap is 16, and the growth pattern must be 2 times the number of points;
HashMap's data structure:


    • Array:
      The array storage interval is contiguous, and you can quickly access any element in the array by subscript, again, if you want to delete an element, you need to move a large number of elements, and the insert operation is the same. But the memory is serious, so the space complexity is very large. However, the binary lookup time of the array is less complex, only O (1). The features of the array are: easy addressing, insertion and removal difficulties.

    • Linked list:
      The storage interval of the list is discrete, the memory is relatively loose, so the space complexity is very small, but the time complexity is very large, O (N). The list is characterized by difficult addressing, easy insertion and deletion.

    • HashMap's Choice: hash table
      Hash tables combine the characteristics of both, making a data structure that is easy to address, and easy to insert and delete. The hash table (hash table) satisfies the convenience of data searching and does not occupy too much content space.

From what we can find is that the hash table consists of an array + linked list, an array of length 16, each of which stores the head node of a linked list. The hash value of the element stored in the array is then obtained by hashing (key)%len, which is the logarithm of the value of the element's key. For example, in a hash table, 12%16=12,28%16=12,108%16=12,140%16=12. So 12, 28, 108, and 140 are all stored in the position labeled 12 below the array.

HashMap implements a static internal class node, which implements the Map.entry interface. The array implemented in HashMap is the contents of Node[],map in node[], as shown in.

Data structure of HashMap in Java

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.