/** * Creates a {@code LinkedHashSet} instance, with a high enough "initial capacity" that it * <i>should</i> hold {@code expectedSize} elements without growth. This behavior cannot be * broadly guaranteed, but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed * that the method isn't inadvertently <i>oversizing</i> the returned set. * * @param expectedSize the number of elements you expect to add to the returned set * @return a new, empty {@code LinkedHashSet} with enough capacity to hold {@code expectedSize} * elements without resizing * @throws IllegalArgumentException if {@code expectedSize} is negative * @since 11.0 */ public static <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize(int expectedSize) { return new LinkedHashSet<E>(Maps.capacity(expectedSize)); }
/** * Creates a {@code HashMap} instance, with a high enough "initial capacity" * that it <i>should</i> hold {@code expectedSize} elements without growth. * This behavior cannot be broadly guaranteed, but it is observed to be true * for OpenJDK 1.7. It also can't be guaranteed that the method isn't * inadvertently <i>oversizing</i> the returned map. * * @param expectedSize the number of entries you expect to add to the * returned map * @return a new, empty {@code HashMap} with enough capacity to hold {@code * expectedSize} entries without resizing * @throws IllegalArgumentException if {@code expectedSize} is negative */ public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) { return new HashMap<K, V>(capacity(expectedSize)); }
/** * Constructs an empty {@code LinkedHashMultimap} with enough capacity to hold * the specified numbers of keys and values without rehashing. * * @param expectedKeys the expected number of distinct keys * @param expectedValuesPerKey the expected average number of values per key * @throws IllegalArgumentException if {@code expectedKeys} or {@code * expectedValuesPerKey} is negative */ public static <K, V> LinkedHashMultimap<K, V> create(int expectedKeys, int expectedValuesPerKey) { return new LinkedHashMultimap<K, V>( Maps.capacity(expectedKeys), Maps.capacity(expectedValuesPerKey)); }
/** * Returns a new hash set using the smallest initial table size that can hold {@code expectedSize} * elements without resizing. Note that this is not what {@link HashSet#HashSet(int)} does, but it * is what most users want and expect it to do. * * <p>This behavior can't be broadly guaranteed, but has been tested with OpenJDK 1.7 and 1.8. * * @param expectedSize the number of elements you expect to add to the returned set * @return a new, empty hash set with enough capacity to hold {@code expectedSize} elements * without resizing * @throws IllegalArgumentException if {@code expectedSize} is negative */ public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) { return new HashSet<E>(Maps.capacity(expectedSize)); }
/** * Creates a {@code LinkedHashMap} instance, with a high enough * "initial capacity" that it <i>should</i> hold {@code expectedSize} * elements without growth. This behavior cannot be broadly guaranteed, but * it is observed to be true for OpenJDK 1.7. It also can't be guaranteed * that the method isn't inadvertently <i>oversizing</i> the returned map. * * @param expectedSize the number of entries you expect to add to the * returned map * @return a new, empty {@code LinkedHashMap} with enough capacity to hold * {@code expectedSize} entries without resizing * @throws IllegalArgumentException if {@code expectedSize} is negative * @since 19.0 */ public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) { return new LinkedHashMap<K, V>(capacity(expectedSize)); }