Here you can find the source of createHashMapWithSize(final int size)
Parameter | Description |
---|---|
KeyType | The type for the key of the map. |
ValueType | The type of the value in the map. |
size | The size. Must be positive. |
public static <KeyType, ValueType> HashMap<KeyType, ValueType> createHashMapWithSize(final int size)
//package com.java2s; /*/*from w ww.j a v a2 s. c om*/ * File: CollectionUtil.java * Authors: Justin Basilico * Company: Sandia National Laboratories * Project: Cognitive Foundry * * Copyright March 25, 2008, Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the U.S. Government. Export * of this program may require a license from the United States Government. * See CopyrightHistory.txt for complete details. * */ import java.util.HashMap; public class Main { /** * The default load factor for a hash map is {@value}. */ private static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * Creates a new {@link HashMap} with the given expected size. It uses the * default load factor (0.75) to estimate the proper number of elements for * the data structure to avoid a rehash or resize when the given number of * elements are added. * * @param <KeyType> The type for the key of the map. * @param <ValueType> The type of the value in the map. * @param size The size. Must be positive. * @return A new hash map with the given expected size. */ public static <KeyType, ValueType> HashMap<KeyType, ValueType> createHashMapWithSize(final int size) { final int initialCapacity = (int) Math.ceil(size / DEFAULT_LOAD_FACTOR); return new HashMap<KeyType, ValueType>(initialCapacity, DEFAULT_LOAD_FACTOR); } }