Solved

Which collection is best for storing key,value performance wise

Posted on 2013-02-06
14
427 Views
Last Modified: 2013-02-17
Hello,

I want to store the properties files having key ,value in java Collection.
Which collection is best for storing such values performance wise.
0
Comment
Question by:Rocking
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38860995
If you mean Properties, then that's already a Collection - a hash table essentially
0
 

Author Comment

by:Rocking
ID: 38862352
Why only hash table?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 38862774
if its not accessed/updated concurrently by multiple threads, you can move them to a HashMap which is more performant than HashTable!
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 26

Expert Comment

by:dpearson
ID: 38862925
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
 
LVL 16

Expert Comment

by:Valeri
ID: 38867673
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
 

Author Comment

by:Rocking
ID: 38876788
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 38877038
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
 
LVL 26

Expert Comment

by:dpearson
ID: 38877377
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 38879039
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
 
LVL 26

Expert Comment

by:dpearson
ID: 38879159
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 38879599
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
 

Author Comment

by:Rocking
ID: 38889346
@dpearson

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

Could you please elaborate a more with some example.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 200 total points
ID: 38895832

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
 

Author Closing Comment

by:Rocking
ID: 38898043
Very helpful in understanding the concept.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 39
Error in @AspectJ Based AOP with Spring 2 13
throw exception 21 44
jsp login check 12 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question