Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-Once + Read-Numerous Map in Java?

I have a requirement that a Map will be constructed with up to 50~200 entries (it could be more, let's call it not-too-little anyway). The writing is only done once and the reading (using Map.get("keyName")) can go more than 20 per request (it's a webapp).

I'm going for Hashmap currently as it (I suppose) gives me the most optimum performance (note: numerous reads per request). Not being a data-structure guy, can any of you suggest a Map implementation that's best fit for my requirement, say from the java.lang.*, Apache commons, etc. packages?

yc

like image 962
yclian Avatar asked Mar 12 '09 04:03

yclian


2 Answers

Unless you are actually having performance problems (and have traced them to this code) I wouldn't worry about it.

And before I tried to replace the Map I'd look at why, exactly, I need to do 4000 lookups (200 entries by 20 reads each) to generate a web page.

But at a bet I'd guess that the time to do those 4000 lookups will turn out to be negligible compared to other parts of the process.

like image 70
MarkusQ Avatar answered Sep 30 '22 03:09

MarkusQ


If all of the writes are done before any of the reads then you could use the Collectons.unmodifiableMap method.

If that isn't the case then writing code to do what you want isn't terribly hard (wanders off to find the post that has the basic code in it...)

Hmm... just to be sure that the question is what I am thinking... is the read only aspect the important part or is trying to access the data fast the most important part?

Edit: (based on comment)

Have you checked to see, with a profiler if the code is slow? If not then you should not be worrying about it yet.

like image 37
TofuBeer Avatar answered Sep 30 '22 03:09

TofuBeer