Which collection is best for storing key,value performance wise

Hello,

I want to store the properties files having key ,value in java Collection.
Which collection is best for storing such values performance wise.
RockingAsked:
Who is Participating?
 
dpearsonConnect With a Mentor Commented:

it uses a single synchronized object to control access to the collection

Both HashTable and Collections.synchronizedMap have methods like this:

public synchronized V get(Object key) {
...
}

When 2 thread encounter this "synchronized" statement, the first locks access to the object (the HashTable in this example) and prevents the 2nd thread from proceeding until the first "get" finishes.

That's reasonable but doesn't produce top performance.  If you had 1000 threads all trying to read from this hash table, each would have to wait for the other 999 before it could make a call to this method.

ConcurrentHashMap works completely differently.  It essentially keeps a lock on each bucket within the collection - so threads only block each other if they are reading/writing from the same bucket (for a large enough map there's only one bucket for each value so you never block).

You don't really need to understand the details of how ConcurrentHashMap works - but there's really no situation where it will be slower than HashTable and lots and lots of situations where it will be much faster.  For that 1000 thread example, normally all 1000 threads will be able to execute at the same time without blocking each other - IF you use a ConcurrentHashMap.

Doug
0
 
CEHJCommented:
If you mean Properties, then that's already a Collection - a hash table essentially
0
 
RockingAuthor Commented:
Why only hash table?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ksivananthCommented:
if its not accessed/updated concurrently by multiple threads, you can move them to a HashMap which is more performant than HashTable!
0
 
dpearsonCommented:
For single threaded use a HashMap has constant time performance when you read from it.

 (That means it doesn't matter how big the map is - it takes a fixed amount of time to get data from it - if you used a List then you'd have to search the list which gets longer, the bigger the list).

For multi threaded use a ConcurrentHashMap which has constant time performance even when multiple threads are accessing it at once.

Doug
0
 
ValeriCommented:
I think that the author of the question is not aware of that there is a special class for that, or may be I'm wrong, I don't know?! Anyway, the class is this one:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
It extends Hashtable which is one of the fastest Collections in multithreading environment. The class itself is threadsafe, so it doesn't need external synchronization.
0
 
RockingAuthor Commented:
In my project which is stable for around 5-6 years,in that HashTable is used,
Is there any plus point with hashtable over hashmap?

That means it doesn't matter how big the map is - it takes a fixed amount of time to get data from it

If we are accessing the map from two different request at different time,then the performance would remain same?
0
 
CEHJCommented:
Is there any plus point with hashtable over hashmap?
It's synchronized by default, which is a good thing if it's being accessed by multiple requests.
If we are accessing the map from two different request at different time,then the performance would remain same?
Access time for each request (one request will be let through since it's synchronized) is a theoretical O(1).
0
 
dpearsonCommented:
I think there's actually no longer a situation where HashTable is a good choice.  That's an old data structure which has been replaced by ConcurrentHashMap for use in multithreaded environments.

If you use HashTable then all threads block each other when accessing the hash table - so the time for each request is actually a function of the number of threads placing requests - making it no longer O(1) in a busy concurrent environment (like a web server).

ConcurrentHashMap is a much more complicated data structure (for Sun/Oracle to write, not for you to use) which allows multiple threads to share the same map without interfering with each other - so it keeps the O(1) performance of a map even in a busy concurrent environment.

For single threaded environments, HashMap is preferable over either HashTable or ConcurrentHashMap as it has no overhead for supporting multiple threads.

Doug
0
 
ksivananthCommented:
It's synchronized by default, which is a good thing if it's being accessed by multiple requests

wrong, its good only if its being accessed concurrently by multiple threads. Also hastable is a legacy implementation, if you need threadsafe maps, you can use Collections.synchronizedMap(Map<K, V>) or ConcurrentHasMap as suggessted earlier!
0
 
dpearsonCommented:
Just to be clear, Collections.synchronizedMap(Map<K, V>) has the same performance problems that HashTable has (it uses a single synchronized object to control access to the collection) and generally should be avoided now that ConcurrentHashMap exists.

Doug
0
 
CEHJCommented:
wrong, its good only if its being accessed concurrently by multiple threads.
I'm assuming multiple requests would be handled by multiple threads
Just to be clear, Collections.synchronizedMap(Map<K, V>) has the same performance problems that HashTable has (it uses a single synchronized object to control access to the collection) and generally should be avoided now that ConcurrentHashMap exists.
That's true. My comment above is simply a literal response to the 'plus points' issue and shouldn't be regarded as an endorsement of Hashtable ;)
0
 
RockingAuthor Commented:
@dpearson

it uses a single synchronized object to control access to the collection

Could you please elaborate a more with some example.
0
 
RockingAuthor Commented:
Very helpful in understanding the concept.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.