S.P Singh
S.P Singh

Reputation: 1289

Better approch to choose fine data structure for picking random key value from a long list [HashMap,LinkedHashMap and ConcurrentHashMap]

Language : Java
Project Type : Web (war application)
Requirement : According to region we need to show string literal from database.

Description: - We have some language specific database tables in which we have mapped string literals as key value pair.

E.g. I) - like for en_US, Table Name: - StringLiteral_en_US 
----------------------------------------------------
 Key                |    Value
 Customer_Name      |     ABC
 Address            |     XYZ

 Values will be continued....around 2000

 E.g. II) - like for en_GB, Table Name: - StringLiteral_en_GB
 ----------------------------------------------------
Key                |    Value
Customer_Name      |     ABC  
Address            |     XYZ  

Values will be continued....around 2000

Tables have maintained properly and we are fetching all the key values from database and store it into data structure [Till now we have chosen LinkedHashMap]

Problem:-

We have problem to choose best data structure to store value and retrieve value in faster way. Because we will be fetch in random basis. Whatever key will be requiring we will fetch that particular key.

So we need to keep maintaining efficiency of program so that whenever key /value will increase. There will not be any impact during random access of any key.

Requirement: -

Please suggest us to choose data structure which can increase product productivity.

All the suggestion will be highly appreciated.

Upvotes: 2

Views: 160

Answers (3)

Chris
Chris

Reputation: 5654

If it is purely for internationalization I don't see a point as to how the insertion order of the Entry matters. So I would say NO to LinkedHashMap

ResourceBundles or these properties are loaded during initialization. And unless there is a runtime reload and various threads fighting to win NO to ConcurrentHashMap as well

HashMap is fast in retrieval and is a potential candidate.

If you would like to create weak references, WeakHashMap is good to look at

If there is a one-to-one key-value mapping and you would like to reverse lookup, Try Bimap

Upvotes: 1

Daniel Kaplan
Daniel Kaplan

Reputation: 67350

Since you're doing this for internationalization, and you care about "product productivity" I would try to get this data into a Resource Bundle some how. It's meant for this purpose.

If you set it up correctly, it'll make your client facing code much easier to implement. As an example, once you set up your resource bundle correctly, you can write this code in a jsp and it'll know how to pick the write value: <h3><fmt:message key="Choose"/></h3>. Without a resource bundle, you'll have to write your own flow control logic by hand. That would be tedious and buggy.

Even if you're not using jsps, your web framework probably knows how to deal with ResourceBundles. If you put this in a naive data structure, your web framework probably won't know how to use it.

Here's a tutorial on how to load a resource bundle with a database.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533500

There is no way to chose one over another. You haven't provided any metrics as to what performance you need, nor what performance you are getting.

Note: creating a String key will be more expensive than looking up the map most of the time.

I suggest you use the simplest implementation which in this case is likely to be HashMap. If you use LinkedHashMap you can see the order the elements were added which might be useful.

Upvotes: 2

Related Questions