3

I'm using Infinispan 5.0.1 for my caching needs.

The problem I'm having is that I need to get data back from the cache in the same order it was put in the cache. For example:

Cache<String, String> myCache = defaultCacheManager.getCache("myCache");
myCache.put("1", "ONE");
myCache.put("2", "TWO");
myCache.put("3", "THREE");
myCache.keySet();
Set<String> keySet = myCache.keySet();
for (String key : keySet) {
    System.out.println(myCache.get(key));
}

This should print out: ONE TWO THREE

kovica
  • 2,443
  • 3
  • 21
  • 25

2 Answers2

0

For the time being I solved this by using ConcurrentLinkedHashMap and it works good. Still, if someone knows the answer to the original question of "How to do it in Infinispan" I'd appreciate the anwser.

kovica
  • 2,443
  • 3
  • 21
  • 25
-2

Why should it print out ONE TWO THREE? keySet() returns a Set, which is an un-ordered collection. Please see the Javadocs for Set, to understand its contract.

Manik Surtani
  • 396
  • 3
  • 12
  • Now here you are wrong. LinkedHashMap says in JavaDoc: "This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order)". And if you don't believe me try this snippet: LinkedHashMap map = new LinkedHashMap<>(); map.put("1", "ONE"); map.put("2", "TWO"); map.put("3", "THREE"); Set keySet = map.keySet(); for (String key : keySet) { System.out.println(key + " = " + map.get(key)); } – kovica Dec 26 '12 at 23:47