Java 其中';s谷歌收藏';那是LazyMap?

Java 其中';s谷歌收藏';那是LazyMap?,java,collections,guava,Java,Collections,Guava,ApacheCommons集合中我最喜欢的一款是LazyMap,它在执行map.get(newKey);//时会使用一个转换器动态实例化值不会返回空值 为什么谷歌收藏没有相同的功能? 它被称为newmapmaker()。makeComputingMap(函数自10.0以来,guava有了一个新类,它与gwt兼容 .我建议你自己写 public class LazyMap<K, V> extends ForwardingMap<K, V> { final Funct

ApacheCommons集合中我最喜欢的一款是LazyMap,它在执行
map.get(newKey);//时会使用一个转换器动态实例化值不会返回空值

为什么谷歌收藏没有相同的功能?


它被称为
newmapmaker()。makeComputingMap(函数自10.0以来,guava有了一个新类,它与gwt兼容


.

我建议你自己写

public class LazyMap<K, V> extends ForwardingMap<K, V> {
    final Function<? super K, ? extends V> factory;
    final Map<K, V> delegate;

    public static <K, V> LazyMap<K, V> lazyMap(final Map<K, V> map, final Supplier<? extends V> supplier) {
        return new LazyMap<>(map, supplier);
    }

    public static <K, V> LazyMap<K, V> lazyMap(final Map<K, V> map, final Function<? super K, ? extends V> factory) {
        return new LazyMap<>(map, factory);
    }

    private LazyMap(final Map<K, V> map, final Function<? super K, ? extends V> factory) {
        this.factory = factory;
        this.delegate = map;
    }

    private LazyMap(final Map<K, V> map, final Supplier<? extends V> supplier) {
        this.factory = Functions.forSupplier(supplier);
        this.delegate = map;
    }

    @Override
    protected Map<K, V> delegate() {
        return delegate;
    }

    @Override
    public V get(final Object key) {
        if (delegate().containsKey(key) == false) {
            @SuppressWarnings("unchecked")
            final K castKey = (K) key;
            final V value = factory.apply(castKey);
            delegate().put(castKey, value);
            return value;
        }
        return delegate().get(key);
    }

}
公共类LazyMap扩展了ForwardingMap{

最终功能主要区别在于CacheBuilder将创建缓存而不是映射。另一个MapBuilder将生成ConcurrentMap,其中一些方法将被弃用,因此…对于在此登录的任何人来说,这已经被弃用,现在我们讨厌使用Chaches。