Solved

Which collection is best for storing key,value performance wise

Posted on 2013-02-06
14
426 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
 
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
DevOps Toolchain Recommendations

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

 
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
split53 challenge 7 78
for i loop in grovy 1 44
Java Jpanels and Jframe 8 28
xampp tool 12 24
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now