0

I have an ArrayList of a custom object A. I need to retrieve 2 variables from A based on certain conditions. Should I simply use for loop to retrieve data from the list each time or create 2 LinkedHashMap and store the required variable in it as key/value pair for faster access later? Which is more efficient? Does creating 2 additional map objects justify the efficiency during search?

List will contain about 100-150 objects so does the two maps. It will be used by concurrent users on daily basis.

yonikawa
  • 581
  • 1
  • 9
  • 32
  • 3
    It depends on how often you do this and how big the Map is likely to be. If it works and is fast enough now, I suggest you leave it, until you know you need to change it. – Peter Lawrey Jun 21 '17 at 17:34
  • When facing the question "is it more efficient to do X or Y?", I find it helpful to simply do benchmark testing. Create some mocked data, jack the numbers up to the millions, and print the time it takes for each test. (Assuming you already understand the underlying structure of the two data structures) – Kylon Tyner Jun 21 '17 at 17:36
  • 1
    "A difference that makes no difference is no difference." - Anonymous. "Premature optimization is the root of all evil." - Donald Knuth. "Make it right first, then make it fast (only) if you need to." - God. – Lew Bloch Jun 21 '17 at 17:38
  • Can you refine the question a bit more? Now we know the size of data, then how often is this data being accessed, does it need to be concurrent, how does the condition looks like (this might give an idea of how to index the object more efficiently for your purpose). I find it hard to give an all in one solution when it needs to be optimized for a specific case. – andreim Jun 21 '17 at 17:48
  • 1
    @Kylon Tyner: I tried testing it out, turned out the performance difference is negligible unless the data is over 2500 objects. So I am going with ArrayList for now. – yonikawa Jun 21 '17 at 21:03

2 Answers2

0

Asking about "efficiency" is like asking about "beauty". What is "efficiency"? I argue that efficiency is what gets the code out soonest without bugs or other misbehavior. What's most efficient in terms of software costs is what saves programmer time, both for initial development and maintenance. In the time it took you to find "answers" on SO, you could have had a correct implementation coded and correct, and still had time to test your alternatives rigorously under controlled conditions to see which made any difference in the program's operation.

If you save 10 ms of program run time at the cost of horridly complex, over-engineered code that is rife with bugs and stupidly difficult to refactor or fix, is that "efficient"?

Furthermore, as phrased, the question is useless on SO. You provided no definition of "efficient" from your context. You provided no information on how the structures in question fit into your project architecture, or the extent of their use, or the size of the problem, or anything else relevant to any definition of "efficiency".

Even if you had, we'd have no more ability to answer such a question than if you asked a roomful of lawyers, "Should I sue so-and-so for what they did?" It all depends. You need advice, if you need advice at all, that is very specific to your situation and the exact circumstances of your development environment and process, your runtime environment, your team, the project goals, budget, and other relevant data.

If you are interested in runtime "efficiency", do the following. Precisely define what exactly you mean by "efficient", including an answer to "how 'efficient' is 'efficient' enough?", and including criteria to measure such "efficiency". Once you have such a precise and (dis)provable definition, then set up a rigorous test protocol to compare the alternatives in your context, and actually measure "efficiency".

When defining "efficiency", make sure that what you define matters. It makes no difference to be "efficient" in an area that has very low project cost or impact, and ignore an area that has huge cost or impact.

Don't expect any meaningful answer for your situation here on SO.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
-2

Use LinkedHashMap because it made for key value pair (according to your requirement).because data will increase in production environment.

  • 1
    And how do you know this is useful, @Abishek Kumar Jain? How much will "decent guy"'s data increase in their production environment, and how do you know? Why `LinkedHashMap` and not `HashMap`? How much difference will it make in the OP's code? How much will it cost them to implement your situation? You don't know any of this, yet you presume to answer the question as if this were the absolute, universal, always-correct solution. No pass. – Lew Bloch Jun 21 '17 at 17:51
  • @LewBloch he needs key value pair and also he asked to choose from LinkedHashMap and list.It means him requirement is for inserting order maintain in map that's why i suggest linkedhashmap.Is it ok. – Abhishek Kumar Jain Jun 22 '17 at 12:10