Is there a synchronized & sorted map object (LinkedHashTable)?

I am creating a servlet.Filter implementation in which I look up the user ID in the database based on the IP address before sending the request to the servlet.

I want my filter to place incoming requests in a map-like object if there is already a request from the same IP address that is being viewed in the database. Then, when I receive a response from the database, I will apply it to all requests to this IP address and send them along the path to the servlet.

A map-like object must be synchronized, but it also maintains the insertion order, so that as soon as I find the user ID, all the requested "requests will be processed in the order in which they were received."

Within the framework of the API, there is a LinkedHashMap that will keep order in order, but not in sync, the HashTable sync, but does not give any indication that it will maintain the correct order.

Is there any LinkedHashTable object that I can use for this?

I am using Java 6.

+4
source share
5 answers

You can use synchronizedMap to transfer any Map implementation to a thread-safe container:

 Map<K, V> synchronizedLinkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>()); 

You can also use ConcurrentSkipListMap if you want to order your items using Comparator .

+7
source

You can make any base card synchronized by wrapping it with java.util.Collections.synchronizedMap (...)

So you would do:

 Map m = Collections.synchronizedMap(new LinkedHashMap()); 
+1
source

Can you use Collections#synchronizedMap(Map) and pass it to LinkedHashMap ? This is the easiest way.

UPDATE: you can also use Guava MapMaker , but I don’t think this is a 100% point.

+1
source

You can sync any card / collection:

 Map<...> m = Collections.synchronizedMap(new LinkedHashMap()); 

There synchronizedSet() , synchronizedList() , etc., there even synchronizedSortedMap() .

+1
source

1. Use a HashTable , which is a synchronized Map between Maps

2. You can also use ConcurrentHashMap .

0
source

All Articles