Java HashMap and javahashmap

Source: Internet
Author: User

Java HashMap and javahashmap
Performance Factor of HashMap

1. Capacity: The number of buckets.

2. Initial Capacity: the number of bits in a table created.

  • If you know how many items will be stored in the HashMap, creating a HashMap with an appropriate initial capacity will avoid the overhead of automatic hash.
/*** The default initial capacity-MUST be a power of two. */static final int DEFAULT_INITIAL_CAPACITY = 1 <4; // default size

3. Size: the number of items currently stored in the table.

 

4. Load Factor: Size/capacity. A table with a small load factor has a low possibility of conflict, and the insert and search speeds are relatively fast (but it slows down the process of traversing using the iterator ). Both HashMap and HashSet have constructors that allow you to specify the load factor, indicating that when the load condition reaches the load factor level, the container will automatically increase the capacity. The implementation method is to double the capacity and allocate existing objects to the new bucket set.

    /**     * The load factor used when none specified in constructor.     */    static final float DEFAULT_LOAD_FACTOR = 0.75f;

 

HashMap Constructor

When creating a HashMap object, no matter which constructor is called

HashMap (int initialCapacity, float loadFactor), initialCapacity is the initial capacity, and loadFactor is the load factor
/*** Constructs an empty <tt> HashMap </tt> with the specified initial * capacity and load factor. ** @ param initialCapacity the initial capacity * @ param loadFactor the load factor * @ throws provided if the initial capacity is negative * or the load factor is nonpositive */public HashMap (int initialCapacity, float loadFactor) {if (initialCapacity <0) throw new capacity ("Illegal initial capacity:" + initialCapacity); if (initialCapacity> MAXIMUM_CAPACITY) // The capacity is initialCapacity = capacity; if (loadFactor <= 0 | Float. isNaN (loadFactor) throw new IllegalArgumentException ("Illegal load factor:" + loadFactor); this. loadFactor = loadFactor; this. threshold = tableSizeFor (initialCapacity); // tableSizeFor (initialCapacity) returns x, x meeting x = 2 ^ n and x> = initialCapacity )}

 

Let's take a look at the implementation of tableSizeFor (I never think of such a tall method)

/*** Returns a power of two size for the given target capacity. */static final int tableSizeFor (int cap) {int n = cap-1; // This is because the cap is an integer power of 2. assume that the highest bit 1 of n is in the I-th bit (the second bit is 0th bits) n | = n >>> 1; // 2. at this time, the binary I of n, the I-1 bit is 1 n | = n> 2; // 3. at this time, n binary I, I-1, I-2, I-3 bits are 1 n | = n> 4; // 4. at this time n binary I, I-1, I-2, I-3, I-4, I-5, i-6, i-7, bits are 1 (of course, the rigorous point should be assumed that I> 7) n | = n> 8; // 5. --------- n | = n >>> 16; // 6. -- ------- Return (n <0 )? 1: (n> = MAXIMUM_CAPACITY )? MAXIMUM_CAPACITY: n + 1 ;}

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.