import java.util.HashMap;
import java.util.HashSet;
public class ComputeIfAbsentWithHashSetNew {
public static void main(String[] args) {
var map = new HashMap<Integer, HashSet<Integer>>();
var data = new int[][]{{305589003, 4136}, {305589004, 4139}};
for (var entry : data) {
int x = entry[0];
int y = entry[1];
// map.computeIfAbsent(x, _x -> new HashSet<>()).add(y); // ----- line 1
var k = map.computeIfAbsent(x, HashSet::new); // ----- line 2
k.add(y);
}
}
}
Above code throws at jdk 17、 18、 19:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.HashMap.resize(HashMap.java:702)
at java.base/java.util.HashMap.putVal(HashMap.java:627)
at java.base/java.util.HashMap.put(HashMap.java:610)
at java.base/java.util.HashSet.add(HashSet.java:221)
at _explore.ComputeIfAbsentWithHashSetNew.main(ComputeIfAbsentWithHashSetNew.java:15)
When debug, I see the newCap in Hashmap::resize() is very large, I don't know why. I thought both the lines do same thing previously.
When I replace line 2 with line 1 in code, it runs successfully.
HashSetwith default capacity, the other creates anHashSetwith initial capacityx, which may very well be 305589003, which is rather big.computeIfAbsentcalls the 2nd parameter (aFunction) with the key as argument, in this case305589003; the passed functionHashSet::newis theHashSet(int initialCapacity)- as commented above,305589003as capacity is too big to be stored in the available memory (line 1 should bek -> new HashSet<>(k)to be the same as line 2, both wrong)