Why should not iterate a hashmap?


I've been doing a project and one of my colleagues mentioned that iterating hashmaps is something to avoid and instead should use hashmap .

However, I think the hashmap's versatility of being able to save strings as a key allows you to do things like:

hash_distrito->hash_cidade->array_coordenadas(x, y, z)
asked by anonymous 10.01.2018 / 21:25

1 answer


When someone tells you what to do, ask for justification.


Iterating any data structure with a dataset has no problem whatsoever, in general.

If you want to iterate in a specific way, in a specific structure, and want a specific result, you may have a problem there.

Essentially, all basic data structures can be iterated in linear (O) linear complexity ( complexity). It just would not be like some very complex and very specific structure that practically nobody uses. Usually can not do it in less time than this, and will not take more. So much so that no one puts what is the complexity for this algorithm, it is "always" the same.

Do you want to iterate in some specific order? Does it need to be in the order that the elements were entered? Or do you need a specific classification? If you need something like that, it's better to use another structure if you can. Maps usually have no definite order. There are even implementations that allow order, but can only be used in specific circumstances, most maps use spreadsheets, which do not allow order .


If you need the order use an array or structure based on it. It's great in the vast majority of needs.

If you need the sorted data then it should be more interesting to use a tree, which is a linked list that has more than one path to follow in sequence.

It is very rare that the pure linked list is useful, usually only when it does not need to be handled after it is created, other than at the endpoint (s) and also does not need random access, which causes other structures are also appropriate.

Linked lists, as you learn in the computer course, are essentially not used in the "real world." More sophisticated implementations, probably combined with other frameworks, may be useful in certain scenarios.

It is very common for people to think that inserting and removing an element from a linked list can be done in constant complexity because the operation itself is actually constant. However, it almost always has to reach the point where the element will be inserted or removed, and then the complexity is linear.

I should be one of the few people who use linked list in database under certain circumstances. I used where it gave me some advantage over the normal index the DB already has. Although today it is rare for me to have this need. You have to weigh everything to decide which structure is best.

Do not evaluate wrong

I've seen people transform an array into a map to be able to index in constant rather than linear time. I do not say that this is useless in all cases, but almost always the complexity happened to be O (N + k) in the best case, or only disadvantage. One forgets to count the copy time of the structure. If it had done in the array would be O (N) in the worst case and O (1) at best.

Perhaps your problem is best suited for a linked list, but we are not sure what this problem is. You may need to use more than one framework together, and one of them is a linked list (perhaps not a traditional one).

10.01.2018 / 22:53