google-collection 的 LazyMap 在哪里?

发布于 2024-08-20 15:46:25 字数 147 浏览 6 评论 0原文

我最喜欢的 apache commons-collections 之一是 LazyMap,它在执行 map.get(newKey); 时使用 Transformer 动态实例化值。 // 不会返回 null!

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

One of my favourites from apache commons-collections was the LazyMap which would use a Transformer to instantiate values on the fly when doing map.get(newKey); // Will not return null!.

Why doesn't google collections have the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘落散花 2024-08-27 15:46:25

嘿看确实如此

它的名字是 new MapMaker().makeComputingMap(Functioncomputer)

太棒了。

请注意,地图制作器是一个工厂 - 您可以制作一个工厂,设置所有对象引用类型、扩展属性(甚至对象过期时间!),然后用一个工厂创建大量计算地图(或其他类型)线路调用。

例如,就像 google-collections 库的几乎所有其他内容一样,它真的很好 - 一旦你弄清楚“它”在哪里

Hey look! It does!

It's called new MapMaker().makeComputingMap(Function<? super K, ? extends V> computer)

Awesome.

Note that map maker is a factory - you can make one, set all the object reference types, expansion properties (and even object expiration time!), and then go about creating lots of computing maps (or other types) with one line calls.

e.g. like pretty much everything else about the google-collections library, it's really good - once you've figured out where 'it' is

耀眼的星火 2024-08-27 15:46:25

从 10.0 开始,guava 有了一个新类 CacheBuilder ,并且它与 gwt 兼容。

这些是差异

since 10.0, guava have a new class CacheBuilder instead, and it's gwt-compatible.

These are the differences.

乞讨 2024-08-27 15:46:25

我建议你自己写

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);
    }

}

I suggest to write your own

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);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文