當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。