当前位置: 首页>>代码示例>>Java>>正文


Java IdentityHashMap.size方法代码示例

本文整理汇总了Java中java.util.IdentityHashMap.size方法的典型用法代码示例。如果您正苦于以下问题:Java IdentityHashMap.size方法的具体用法?Java IdentityHashMap.size怎么用?Java IdentityHashMap.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.IdentityHashMap的用法示例。


在下文中一共展示了IdentityHashMap.size方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeIdentityHashMap

import java.util.IdentityHashMap; //导入方法依赖的package包/类
/**
 * Writes a <code>IdentityHashMap</code> to a <code>DataOutput</code>. Note that even though
 * <code>map</code> may be an instance of a subclass of <code>IdentityHashMap</code>,
 * <code>readIdentityHashMap</code> will always return an instance of
 * <code>IdentityHashMap</code>, <B>not</B> an instance of the subclass. To preserve the class
 * type of <code>map</code>, {@link #writeObject(Object, DataOutput)} should be used for data
 * serialization.
 *
 * @throws IOException A problem occurs while writing to <code>out</code>
 *
 * @see #readIdentityHashMap
 */
public static void writeIdentityHashMap(IdentityHashMap<?, ?> map, DataOutput out)
    throws IOException {

  InternalDataSerializer.checkOut(out);

  int size;
  if (map == null) {
    size = -1;
  } else {
    size = map.size();
  }
  InternalDataSerializer.writeArrayLength(size, out);
  if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
    logger.trace(LogMarker.SERIALIZER, "Writing IdentityHashMap with {} elements: {}", size, map);
  }
  if (size > 0) {
    for (Map.Entry<?, ?> entry : map.entrySet()) {
      writeObject(entry.getKey(), out);
      writeObject(entry.getValue(), out);
    }
  }
}
 
开发者ID:ampool,项目名称:monarch,代码行数:35,代码来源:DataSerializer.java

示例2: threshold

import java.util.IdentityHashMap; //导入方法依赖的package包/类
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:12,代码来源:Capacity.java

示例3: ofIdentityHashMap

import java.util.IdentityHashMap; //导入方法依赖的package包/类
/**
 * Checks adequacy of the expected maximum size of a static field
 * of type {@code IdentityHashMap}.
 *
 * Having
 * <pre>
 * class XClass {
 *     static IdentityHashMap theMap = new IdentityHashMap(M);
 * }
 * </pre>
 *
 * you should call from the test
 *
 * <pre>
 * OptimalCapacity.ofIdentityHashMap(XClass.class, "theMap", M);
 * </pre>
 */
public static void ofIdentityHashMap(Class<?> clazz, String fieldName,
        int expectedMaxSize)
{
    try {
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        Object obj = field.get(null);
        if (!IdentityHashMap.class.equals(obj.getClass())) {
            throw new RuntimeException("'" + field +
                "' expected to be of type IdentityHashMap");
        }
        IdentityHashMap<?,?> map = (IdentityHashMap<?,?>)obj;

        // Check that size of map is what was expected
        if (map.size() != expectedMaxSize) {
            throw new RuntimeException("Size of '" + field +
                "' is " + map.size() +
                ", which differs from expected " + expectedMaxSize);
        }

        // Check that the map allocated only necessary amount of memory
        IdentityHashMap<Object, Object> tmp = new IdentityHashMap<>(map);
        if (internalArraySize(map) != internalArraySize(tmp)) {
            throw new RuntimeException("Final capacity of '" + field +
                "' is " + internalArraySize(map) +
                ", which exceeds necessary minimum " + internalArraySize(tmp));
        }

        // Check that map was initially properly sized
        tmp = new IdentityHashMap<>(expectedMaxSize);
        tmp.put(new Object(), new Object()); // trigger storage init
        if (internalArraySize(map) != internalArraySize(tmp)) {
            throw new RuntimeException("Requested number of elements in '" + field +
                "' was " + expectedMaxSize +
                ", which resulted in final capacity " + internalArraySize(tmp) +
                ", which differs from necessary minimum " + internalArraySize(map));
        }
    } catch (ReflectiveOperationException roe) {
        throw new RuntimeException(roe);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:59,代码来源:OptimalCapacity.java


注:本文中的java.util.IdentityHashMap.size方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。