Here you can find the source of newHashMap(final int size)
size
elements without causing a rehash.
Parameter | Description |
---|---|
K | type of the keys |
V | type of the values |
size | number of elements to accommodate without a rehash |
size
elements without a rehash
public static <K, V> HashMap<K, V> newHashMap(final int size)
//package com.java2s; //License from project: Apache License import java.util.HashMap; public class Main { /**/*from w ww . j a va 2s . c om*/ * Returns a new {@link HashMap} initialized with a large enough capacity to allow adding <code>size</code> elements without causing a rehash. * * @param <K> type of the keys * @param <V> type of the values * @param size number of elements to accommodate without a rehash * @return a new {@link HashMap} with a large enough capacity to accommodate <code>size</code> elements without a rehash */ public static <K, V> HashMap<K, V> newHashMap(final int size) { return new HashMap<>(hashCapacityForSize(size)); } /** * Returns the minimal capacity of hash-based structures (e.g. {@link HashSet} or {@link HashMap}) calculated from the specified size (number of elements) * and the default load factor (which is 0.75). * * @param size size to calculate the minimal capacity * @return the minimal capacity of hash-based structures for the specified size */ public static int hashCapacityForSize(final int size) { return size * 4 / 3 + 1; } }