Returns a capacity that is sufficient to keep the map from being resized as long as it grows no larger than expectedSize and the load factor is >= its default (0.75). - Android java.util

Android examples for java.util:Map

Description

Returns a capacity that is sufficient to keep the map from being resized as long as it grows no larger than expectedSize and the load factor is >= its default (0.75).

Demo Code


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class Main{
    public static void main(String[] argv){
        int expectedSize = 42;
        System.out.println(mapCapacity(expectedSize));
    }/* w  w w  . j av a 2 s .co  m*/
    /**
     * The largest power of two that can be represented as an {@code int}.
     *
     * @since 10.0
     */
    public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
    /**
     * Returns a capacity that is sufficient to keep the map from being resized as long as it grows
     * no larger than expectedSize and the load factor is >= its default (0.75).
     */
    static int mapCapacity(int expectedSize) {
        if (expectedSize < 3) {
            Preconditions.checkArgument(expectedSize >= 0,
                    "Size must be nonnegative but was " + expectedSize);
            return expectedSize + 1;
        }
        if (expectedSize < MAX_POWER_OF_TWO) {
            return expectedSize + expectedSize / 3;
        }
        return Integer.MAX_VALUE; // any large value
    }
}

Related Tutorials