diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index 6430e23d4..d16ae0435 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -824,7 +824,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, keyLengths); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue; @@ -836,6 +837,12 @@ public class RocksDB extends RocksObject { return keyValueMap; } + private static int computeCapacityHint(final int estimatedNumberOfItems) { + // Default load factor for HashMap is 0.75, so N * 1.5 will be at the load + // limit. We add +1 for a buffer. + return (int)Math.ceil(estimatedNumberOfItems * 1.5 + 1.0); + } + /** * Returns a map of keys for which values were found in DB. *

@@ -880,7 +887,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, keyLengths, cfHandles); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if (values[i] == null) { continue; @@ -915,7 +923,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, keysArray, keyOffsets, keyLengths); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue; @@ -971,7 +980,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, keysArray, keyOffsets, keyLengths, cfHandles); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap + = new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue;