ConcurrentLinkedHashMap L

43 views
Skip to first unread message

Vlad

unread,
Oct 19, 2011, 10:54:25 PM10/19/11
to ConcurrentLinkedHashMap, vsv...@gmail.com
I wrote the test and have question in regards to implementation of
ConcurrentLinkedHashMap

package collection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;


public class ConcurrentHashMapExample {

public static void main(String[] args) {

//ConcurrentHashMap
Map<String,String> myMap = new
ConcurrentLinkedHashMap.Builder<String,
String>().maximumWeightedCapacity(1000).build();

myMap.put("1", "1");
myMap.put("2", "1");
myMap.put("3", "1");
myMap.put("4", "1");
myMap.put("5", "1");
myMap.put("6", "1");
System.out.println("ConcurrentLinkedHashMap: "+myMap);
Iterator<String> it = myMap.keySet().iterator();



//HashMap
myMap = new LinkedHashMap<String,String>();
myMap.put("1", "1");
myMap.put("2", "1");
myMap.put("3", "1");
myMap.put("4", "1");
myMap.put("5", "1");
myMap.put("6", "1");
System.out.println("LinkedHashMap: "+myMap);


//HashMap
myMap = new HashMap<String,String>();
myMap.put("1", "1");
myMap.put("2", "1");
myMap.put("3", "1");
myMap.put("4", "1");
myMap.put("5", "1");
myMap.put("6", "1");
System.out.println("HashMap: "+myMap);

}
}

-----------------------------------------------------------------------------------------------------------
ConcurrentLinkedHashMap: {1=1, 5=1, 6=1, 3=1, 4=1, 2=1}
LinkedHashMap: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
HashMap: {3=1, 2=1, 1=1, 6=1, 5=1, 4=1}
-----------------------------------------------------------------------------------------------------------

Why the elements in ConcurrentLinkedHashMap were not printed in FIFO
order. It works for LinkedHashMap very well. How to use
ConcurrentLinkedHashMap for FIFO list?



Patricio Echagüe

unread,
Oct 19, 2011, 11:10:31 PM10/19/11
to concurrentl...@googlegroups.com, vsv...@gmail.com
From the javadoc:

Like Hashtable but unlike HashMap, this class does not allow null to be used as a key or value. Unlike LinkedHashMap, this class does not provide predictable iteration order. A snapshot of the keys and entries may be obtained in ascending and descending order of retention. 

Ben Manes

unread,
Oct 19, 2011, 11:22:58 PM10/19/11
to ConcurrentLinkedHashMap
Hi,
CLHM does not provide deterministic iteration order. The views from
keySet(), values(), and entrySet() delegate to the underlying hash
table and require no synchronization. The snapshot iterators
(ascending and descending views) provide the order that entries will
be evicted. This is intended for serializing the cache when shutting
down a server as the operations are O(n) copies. This ordering is not
explicit to allow the eviction policy to be modified between releases,
so an LRU expectation would break when LIRS is provided instead. This
is stated in the class documentation as,

"Like Hashtable but unlike HashMap, this class does not allow null to
be used as a key or value. Unlike LinkedHashMap, this class does not
provide predictable iteration order. A snapshot of the keys and
entries may be obtained in ascending and descending order of
retention."

If you require a ConcurrentMap in FIFO order then there are a number
of alternative approaches that are reasonable. It is not provided in
CLHM due to being out of scope for caching and adding extra overhead
that is undesirable in the targeted use-case (caches).
Reply all
Reply to author
Forward
0 new messages