Solved

Which collection is best for storing key,value performance wise

Posted on 2013-02-06
14
425 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
Issues installing SSL certificate into Apache Tomcat 3 70
return in catch statement 1 40
count11 challenge 6 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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