Java HashMap Create createHashMapWithSize(final int size)

Here you can find the source of createHashMapWithSize(final int size)

Description

Creates a new HashMap with the given expected size.

License

Open Source License

Parameter

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.

Return

A new hash map with the given expected size.

Declaration

public static <KeyType, ValueType> HashMap<KeyType, ValueType> createHashMapWithSize(final int size) 

Method Source Code

//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);
    }
}

Related

  1. createHashMap(int initialCapacity)
  2. createHashMap(int initialCapacity)
  3. createHashMap(int size)
  4. createHashMap(Object key, Object value)
  5. createHashMapIfNull(Map map)
  6. getHashMap()
  7. getHashMap()
  8. getHashMap()
  9. getHashMap()