可以按插入/访问顺序迭代番石榴缓存吗? [英] Is it possible to Iterate over a guava Cache in order of insertion/access?

查看:124
本文介绍了可以按插入/访问顺序迭代番石榴缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Guava Cache替代 ConcurrentLinkedHashMap 。然而,我发现ConcurrentLinkedHashMap允许我按照Insertion的顺序遍历地图,Guava的 asMap()方法不会以任何特定的顺序返回元素。我缺少某些东西,还是这个功能根本不可用?



示例(尝试打印密钥,值和条目):

 缓存<整数,整数> cache = CacheBuilder.newBuilder()。maximumSize(10).initialCapacity(10)
.expireAfterAccess(10000,TimeUnit.SECONDS).build();

cache.put(1,1);
cache.put(2,2);
cache.put(3,3);
cache.put(4,4);
cache.put(5,5);
cache.put(6,6);

迭代器<整数> iter1 = cache.asMap()。keySet()。iterator();

System.out.println(Keys);
while(iter1.hasNext())
System.out.println(iter1.next());

System.out.println(Values);
迭代器<整数> iter2 = cache.asMap()。values()。iterator();

while(iter2.hasNext())
System.out.println(iter2.next());

System.out.println(Entries);
迭代器<条目<整数,整数>> iter3 = cache.asMap()。entrySet()。iterator();

while(iter3.hasNext())
{
条目<整数,整数> entry = iter3.next();
System.out.println(entry.getKey()++ entry.getValue());
}

打印:

 
2
6
1
4
3
5
价值
2
6
1
4
3
5
条目
2 2
6 6
1 1
4 4
3 3
5 5


解决方案

(回答我自己的问题)



似乎fge的答案是正确的,Guava Cache根据插入次序不能重复。作为解决方法,我使用了前面提到的并发链接的HashMap ,它不太丰富,但允许对于Guava团队来说,我还是很欣赏一个人的正式答复,因为这似乎表明ConcurrentLinkedHashMap并没有完全融入到番石榴(与...相反) ConcurrentLinkedHashMap文档)


I'm trying to use a Guava Cache as a replacement for the ConcurrentLinkedHashMap. However I found that while the ConcurrentLinkedHashMap allowed me to iterate over the map in order of Insertion, Guava's asMap() method doesn't return elements in any particular order. Am I missing something, or is this functionality simply not available?

Example (trying to print the keys, the values, and the entries):

Cache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(10).initialCapacity(10)
            .expireAfterAccess(10000, TimeUnit.SECONDS).build();

    cache.put(1, 1);
    cache.put(2, 2);
    cache.put(3, 3);
    cache.put(4, 4);
    cache.put(5, 5);
    cache.put(6, 6);

    Iterator<Integer> iter1 = cache.asMap().keySet().iterator();

    System.out.println("Keys");
    while (iter1.hasNext())
        System.out.println(iter1.next());

    System.out.println("Values");
    Iterator<Integer> iter2 = cache.asMap().values().iterator();

    while (iter2.hasNext())
        System.out.println(iter2.next());

    System.out.println("Entries");
    Iterator<Entry<Integer, Integer>> iter3 = cache.asMap().entrySet().iterator();

    while (iter3.hasNext())
    {
        Entry<Integer,Integer> entry = iter3.next();
        System.out.println(entry.getKey() + " " + entry.getValue());
    }

Prints:

Keys
2
6
1
4
3
5
Values
2
6
1
4
3
5
Entries
2 2
6 6
1 1
4 4
3 3
5 5

解决方案

(Answering my own question)

It seems fge's answer is correct, and the Guava Cache cannot be iterated according to the order of insertion. As a workaround, I used the previously noted ConcurrentLinkedHashMap, which is less feature rich, but allows for ordered iteration.

I'd still appreciate an official answer from someone on the Guava team since this seems to indicate that the ConcurrentLinkedHashMap is not fully integrated into Guava (contrary to the ConcurrentLinkedHashMap documentation)

这篇关于可以按插入/访问顺序迭代番石榴缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆