排序的计算图?

发布于 2024-10-09 06:58:29 字数 188 浏览 0 评论 0原文

如何在 之上构建 SortedMap Guava 的计算地图(或反之亦然)?我想要排序的映射键以及动态计算值。

How can I construct a SortedMap on top of Guava's computing map (or vice versa)? I want the sorted map keys as well as computing values on-the-fly.

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

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

发布评论

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

评论(3

软糖 2024-10-16 06:58:29

最简单的可能是使用 ConcurrentSkipListMap 和 memoizer 惯用法(请参阅 JCiP),而不是依赖于 MapMaker 中预先构建的未排序类型。您可以用作基础的示例是装饰器实现。

The simplest is probably to use a ConcurrentSkipListMap and the memoizer idiom (see JCiP), rather than relying on the pre-built unsorted types from MapMaker. An example that you could use as a basis is a decorator implementation.

挽你眉间 2024-10-16 06:58:29

也许你可以做这样的事情。这不是一个完整的实现。只是一个传达想法的示例。

public class SortedComputingMap<K, V> extends TreeMap<K, V> {
    private Function<K, V> function;
    private int maxSize;

    public SortedComputingMap(int maxSize, Function<K, V> function) {
        this.function = function;
        this.maxSize = maxSize;
    }

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V get(Object key) {
        V tmp = null;
        K Key = (K) key;
        if ((tmp = super.get(key)) == null) {

            super.put(Key, function.apply(Key));
        }
        if (size() > maxSize)
            pollFirstEntry();
        return tmp;
    }

    public static void main(String[] args) {
        Map<Integer, Long> sortedMap = new SortedComputingMap<Integer, Long>(3,
                new Function<Integer, Long>() {
                    @Override
                    public Long apply(Integer n) {
                        Long fact = 1l;
                        while (n != 0)
                            fact *= n--;
                        return fact;
                    }
                });

        sortedMap.get(12);
        sortedMap.get(1);
        sortedMap.get(2);
        sortedMap.get(5);

        System.out.println(sortedMap.entrySet());

    }

}

May be you can do something like this.It's not a complete implementation.Just a sample to convey the idea.

public class SortedComputingMap<K, V> extends TreeMap<K, V> {
    private Function<K, V> function;
    private int maxSize;

    public SortedComputingMap(int maxSize, Function<K, V> function) {
        this.function = function;
        this.maxSize = maxSize;
    }

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V get(Object key) {
        V tmp = null;
        K Key = (K) key;
        if ((tmp = super.get(key)) == null) {

            super.put(Key, function.apply(Key));
        }
        if (size() > maxSize)
            pollFirstEntry();
        return tmp;
    }

    public static void main(String[] args) {
        Map<Integer, Long> sortedMap = new SortedComputingMap<Integer, Long>(3,
                new Function<Integer, Long>() {
                    @Override
                    public Long apply(Integer n) {
                        Long fact = 1l;
                        while (n != 0)
                            fact *= n--;
                        return fact;
                    }
                });

        sortedMap.get(12);
        sortedMap.get(1);
        sortedMap.get(2);
        sortedMap.get(5);

        System.out.println(sortedMap.entrySet());

    }

}
静待花开 2024-10-16 06:58:29

如果您需要线程安全,这可能会很棘手,但如果您不需要,我会推荐一些接近 Emil 建议的方法,但使用 ForwardingSortedMap 而不是扩展 TreeMap直接地。

If you need the thread safety, this could be tricky, but if you don't I'd recommend something close to Emil's suggestion, but using a ForwardingSortedMap rather than extending TreeMap directly.

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