本文整理匯總了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);
}
}
}
示例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;
}
示例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);
}
}