com.google.common.collect.Maps.capacity()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(152)

本文整理了Java中com.google.common.collect.Maps.capacity()方法的一些代码示例,展示了Maps.capacity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.capacity()方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:capacity

Maps.capacity介绍

[英]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).
[中]返回一个容量,该容量足以使贴图不被调整大小,只要它的大小不超过expectedSize,并且加载因子>=其默认值(0.75)。

代码示例

代码示例来源:origin: google/guava

/**
 * 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<>(capacity(expectedSize));
}

代码示例来源:origin: google/guava

/**
 * 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<>(capacity(expectedSize));
}

代码示例来源:origin: google/guava

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

代码示例来源:origin: google/guava

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

代码示例来源:origin: google/guava

/**
 * 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<>(
   Maps.capacity(expectedKeys), Maps.capacity(expectedValuesPerKey));
}

代码示例来源:origin: google/j2objc

/**
 * 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<>(
   Maps.capacity(expectedKeys), Maps.capacity(expectedValuesPerKey));
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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<>(capacity(expectedSize));
}

代码示例来源:origin: wildfly/wildfly

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

代码示例来源:origin: wildfly/wildfly

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

代码示例来源:origin: wildfly/wildfly

/**
 * 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<>(capacity(expectedSize));
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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<>(
   Maps.capacity(expectedKeys), Maps.capacity(expectedValuesPerKey));
}

代码示例来源:origin: google/guava

public void testCapacityForNegativeSizeFails() {
 try {
  Maps.capacity(-1);
  fail("Negative expected size must result in IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: google/guava

public void testCapacityForLargeSizes() {
 int[] largeExpectedSizes =
   new int[] {
    Integer.MAX_VALUE / 2 - 1,
    Integer.MAX_VALUE / 2,
    Integer.MAX_VALUE / 2 + 1,
    Integer.MAX_VALUE - 1,
    Integer.MAX_VALUE
   };
 for (int expectedSize : largeExpectedSizes) {
  int capacity = Maps.capacity(expectedSize);
  assertTrue(
    "capacity (" + capacity + ") must be >= expectedSize (" + expectedSize + ")",
    capacity >= expectedSize);
 }
}

代码示例来源:origin: com.google.collections/google-collections

/**
 * {@inheritDoc}
 *
 * <p>Creates an empty {@code LinkedHashSet} for a collection of values for
 * one key.
 *
 * @return a new {@code LinkedHashSet} containing a collection of values for
 *     one key
 */
@Override Set<V> createCollection() {
 return new LinkedHashSet<V>(Maps.capacity(expectedValuesPerKey));
}

代码示例来源:origin: com.google.collections/google-collections

/**
 * Creates an empty {@code HashSet} instance with enough capacity to hold the
 * specified number of elements without rehashing.
 *
 * @param expectedSize the expected size
 * @return a new, empty {@code HashSet} with enough capacity to hold {@code
 *     expectedSize} elements without rehashing
 * @throws IllegalArgumentException if {@code expectedSize} is negative
 */
public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) {
 return new HashSet<E>(Maps.capacity(expectedSize));
}

代码示例来源:origin: com.atlassian.bundles/google-collections

/**
 * Creates an empty {@code HashSet} instance with enough capacity to hold the
 * specified number of elements without rehashing.
 *
 * @param expectedSize the expected size
 * @return a new, empty {@code HashSet} with enough capacity to hold {@code
 *     expectedSize} elements without rehashing
 * @throws IllegalArgumentException if {@code expectedSize} is negative
 */
public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) {
 return new HashSet<E>(Maps.capacity(expectedSize));
}

代码示例来源:origin: com.google.collections/google-collections

private LinkedHashMultimap(Multimap<? extends K, ? extends V> multimap) {
 super(new LinkedHashMap<K, Collection<V>>(
   Maps.capacity(multimap.keySet().size())));
 linkedEntries
   = new LinkedHashSet<Map.Entry<K, V>>(Maps.capacity(multimap.size()));
 putAll(multimap);
}

代码示例来源:origin: com.atlassian.bundles/google-collections

private LinkedHashMultimap(Multimap<? extends K, ? extends V> multimap) {
 super(new LinkedHashMap<K, Collection<V>>(
   Maps.capacity(multimap.keySet().size())));
 linkedEntries
   = new LinkedHashSet<Map.Entry<K, V>>(Maps.capacity(multimap.size()));
 putAll(multimap);
}

代码示例来源:origin: com.google.collections/google-collections

private void readObject(ObjectInputStream stream)
  throws IOException, ClassNotFoundException {
 stream.defaultReadObject();
 int distinctElements = Serialization.readCount(stream);
 setBackingMap(new LinkedHashMap<E, AtomicInteger>(
   Maps.capacity(distinctElements)));
 Serialization.populateMultiset(this, stream, distinctElements);
}

代码示例来源:origin: com.atlassian.bundles/google-collections

private void readObject(ObjectInputStream stream)
  throws IOException, ClassNotFoundException {
 stream.defaultReadObject();
 int distinctElements = Serialization.readCount(stream);
 setBackingMap(new LinkedHashMap<E, AtomicInteger>(
   Maps.capacity(distinctElements)));
 Serialization.populateMultiset(this, stream, distinctElements);
}

相关文章