HashMap Source Code Analysis

Source: Internet
Author: User
The HashMap in the JDK is one of the most commonly used hash table-based map implementations. With a bit of a feature:
  • Both key and value are allowed to be null.
  • Non-thread safe.
  • Default capacity length:1 << 4 = 16
  • Default maximum capacity 1 << 30
  • Default Factor loadfactor:0.75f
HashMap Idea of realization:
  • Creates an array of the specified capacity to store linked list objects
  • The key to the content is stored by the hash algorithm to obtain the corresponding hash value
  • Length-1 the hash value with the array length to get the key in the array subscript
  • If the location does not have a linked list object, the new linked list is inserted, and NULL is returned
  • If a linked list object exists at that location and no specified key exists, the content is inserted at the end of the list, returning NULL
  • If a linked list object exists at that location and the specified key exists, the content is returned when the contents are replaced
  • size++
  • When size >= threshold and the position of the key is not NULL, the capacity is expanded to 2 * length * loadfactor

HashMap Source Code Analysis

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.