简体   繁体   中英

List iteration, which is the most efficient?

I am working on a game and have several customers stored in an ArrayList, and each customer has their own unique ID which is saved as a variable inside the object.

If I want to retrieve a customer from the list using their ID, which of these would be better practice?

  1. Iterate through all customers in the list until I find a match.

  2. Convert the list to a HashMap of keys (the ID) and values (the customers), and then simply use the .get() method. Maybe this does exactly the same as option one?

HashMap will be more efficient (in most cases O(1), in worst case O(n)), iterating over a list would be O(n).

Of course it depends on the size of your data, if you have lots of it then HashMap is the obvious choice, if you have few (eg 5 maybe 10), it might be the case that a List would be more efficient - constant factors have to be taken under consideration here, which Big-Oh notation ignores.

As written by others, collections having "hash" in their name typically provide improved performance properties for certain kind of actions.

But please keep in mind the very old rule to not do premature optimization. Seriously: unless we are talking a "real production setup"; and 10 000+ paying customers ... then you should understand that you should very much more focus on good design, and creating readable + maintainable code than on potential performance issues.

Yes, one should avoid outright stupid designs; but the thing is: if you focus too much and too early on performance, you risk two things:

a) missing the real bottleneck. If you really encounter performance issues, you have profile your application to understand where time is spent. Far too often people assume that their problem is X; and then they spend a lot of time fixing X - to later find out that actually the Just-in-Time compiler addresses X good enough; and that their real problem is some other Y they never thought of.

b) putting yourself into a corner. Thing is: good designs need to be able to evolve and change. If you assume you must do this or that to preserve some holy performance grail ... chances are, that this impacts your overall application in a negative way.

The hashmap and all sorted list can use the index to find the right item faster. It can pick an index in the middle, if the value it's looking for is higher or lower it can use that information and pick a new index in the lower or higher half and repeat the same task as before. This reduce the time to find the item. It's called binary search (thx fabian) You can try it and compare the result!

EDIT: I am with Jägermeister with this one, you should wait to up the performance and have it as a final step.

I know your question was specific, but I'm going to propose another solution.

Build you own set, backed by an array of Costumers. Once a Costumer is inserted, the Customer's ID is set to the actual index it gets when placed into the array.

that way you can have direct access to all you costumers, using their ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM