0

Language : Java
Project Type : Web (war application)
Requirement : Maintain a Data structure which can contain key and value pair

Description : We have a database table in which we have mapped key, value pair

E.g. I) - Like for Customer Information, Table Name: - Customer_Info
----------------------------------------------------
 Key                |    Value
 Customer_Name      |     ABC
 Address            |     XYZ

continued....around 2000

We fetch customer information from data base and maintain it in a data structure. After that during use we fetch the value correspondent to key.

Till now we have chosen LinkedHashMap for maintaining key,value pair.

Problem:-

But I think here is some problem in data structure selection. 

Please explain the two scenario if I choose
LinkedHashMap, HashMap or ConcurrentHashMap . Which Data Structure will be the appropriate ?

Case I - If I maintain 2000 key values data structure.

Case II- If I fetch random key from whole map from anywhere.

Requirement: -

We have to maintain cost of Execution, Efficiency and increase the productivity of product. Please assist us to choose best data structure.

All the suggestion will be highly appreciated.

S.P Singh
  • 1,267
  • 3
  • 17
  • 23
  • meaning for each customer you are having the Map object ? – ajduke May 27 '13 at 17:00
  • 1
    "Efficiency" here is not nearly as relevant as just choosing the data structure that does what you need and no more. Use `LinkedHashMap` if order matters, `ConcurrentHashMap` if you need concurrency, and `HashMap` if you don't care about either. – Louis Wasserman May 27 '13 at 17:00
  • are you trying to use the "Cache". with use of Map ? – ajduke May 27 '13 at 17:03
  • What do you mean by "Execution"? A particular type of Map is typically chosen based on your requirement on the complexity of inserting, deleting, iterating, etc. One way to get more helpful answers would be to describe the scenario you intend to use the Map. – rethab May 27 '13 at 17:06
  • Yes we are having separate map object for each customer, but I think after all the description, I think I should go for HashMap – S.P Singh May 28 '13 at 16:12

2 Answers2

2

Apart from the fact that the only answer that matters here is the one given by profiling and benchmarking the code, I don't see how you could compare these three data structures since they're made for different purposes:

  • HashMap is the standard, non thread safe
  • ConcurrentHashMap is the one you will be use in a multi threaded environment (read: slower)
  • LinkedHashMap is a normal map which provides a way to iterate over it (following insertion order)

So, theoretically the fastest is the HashMap, since it doesn't have the overhead of the other two, but the comparison here doesn't mean anything.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Here I tried to mention all three, because all three Data Structures are able to store data in the form of key, value pair. And our concern is same to store data in key, value but we need to insert data with proper profiling and cost execution without maintaining order and fetch in fast/random way. – S.P Singh May 28 '13 at 16:18
0

I would go for HashMap which will reduce synchronization cost. Key/Value pairs seems to be static data to i dont need synchronization and i dont see nay reason to use LinkedHashMap as maintaining order is of no use.

Lokesh
  • 7,810
  • 6
  • 48
  • 78