performance - Datastructure : hashmap vs linkedhashmap vs treemap -


i saw there lot of questions differences between these three. tell me if wrong, if sum read:

  • hashmap : more efficient in general; o(1) (normal case), o(n) (worst case, bad hash algorithm)
  • linkedhashmap : maintains insertion order of elements, take more memory hashmap
  • treemap : maintains elements sorted, o(log(n)) (when balanced)

my question : why need maintain insertion order or elements sorted, if @ end performance insert, lookup.. better hashmap?

we need main insertion order because need insertion order solve problem @ hand. sounds bit tautological, that's pretty reason. data ordered benefits random access.

likewise sorting. gives way find "next" or "previous" item cheaply, while still being reasonably efficient arbitrary lookup. 1 example approximate lookups, say, know entry you're looking starts foo don't know rest of key is.

hashmap exists make things faster , simpler (no concept of order) when don't need of these operations, exact lookup.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -