Is it possible to reuse the guava cache in insert / access order?

I am trying to use Guava Cache as a replacement for ConcurrentLinkedHashMap . However, I found that although ConcurrentLinkedHashMap allows me to iterate over the map in insertion order, the Guava method asMap()does not return elements in any particular order. Am I missing something, or is this feature simply not available?

Example (attempt to print keys, values, and records):

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

Print

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
+3
source share
1 answer

(Answering my own question)

, fge , Guava . ConcurrentLinkedHashMap, , .

- - Guava, , ConcurrentLinkedHashMap Guava ( ConcurrentLinkedHashMap)

+1

Source: https://habr.com/ru/post/1667304/


All Articles