我尝试使用MapMaker/CacheBuilder创建缓存,但我不明白如何正确处理空值.
ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
Run Code Online (Sandbox Code Playgroud)
如果createExpensiveGraph方法返回null值,则抛出NullpointerException.我不明白为什么ComputingConcurrentHashMap会抛出一个NPE而不是只返回一个空值.
如何妥善处理?只是捕获NPE并返回null?我错过了什么吗?