Java HashMap Create newHashMap(int size)

Here you can find the source of newHashMap(int size)

Description

new Hash Map

License

LGPL

Declaration

public static <K, V> HashMap<K, V> newHashMap(int size) 

Method Source Code

//package com.java2s;
/*/*from   w ww .jav  a2s . co  m*/
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.util.HashMap;

public class Main {
    public static <K, V> HashMap<K, V> newHashMap(int size) {
        return new HashMap<K, V>(getInitialCapacityFromExpectedSize(size));
    }

    /**
     * As the default loadFactor is of 0.75, we need to calculate the initial capacity from the expected size to avoid
     * resizing the collection when we populate the collection with all the initial elements. We use a calculation
     * similar to what is done in {@link HashMap#putAll(Map)}.
     *
     * @param expectedSize the expected size of the collection
     * @return the initial capacity of the collection
     */
    private static int getInitialCapacityFromExpectedSize(int expectedSize) {
        if (expectedSize < 3) {
            return expectedSize + 1;
        }
        return (int) ((float) expectedSize / 0.75f + 1.0f);
    }
}

Related

  1. newHashMap()
  2. newHashMap(final int initCapacity)
  3. newHashMap(final int size)
  4. newHashMap(final K key, final V value)
  5. newHashMap(int capacity)
  6. newHashMap(int size)
  7. newHashMap(int size)
  8. newHashMap(K[] keys, V[] values)
  9. newHashMap(K[] keys, V[] values)