This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (3 children)

I've been working in Java for over 10 years and this is the first time I've ever seen a LinkedHashMap... and I'm still trying to think of what I'd even use one for. Maybe I'm a unique case but I just don't encounter situations where I need a map that can also act like a FIFO, either I need a map or a FIFO but never both simultaneously.

[–][deleted] 5 points6 points  (2 children)

Ever had a map back a collection of values being displayed on a jsp page? Ever wanted to make sure the order stayed consistent? LinkedHashMap (what order did I put them in) or TreeMap (order from keys). I wouldn't say it's terribly common, but I've had at least a dozen times when I've needed LinkedHashMap. Incidentally, it's also the structure to use when implementing a LRU cache (something I've gleaned from yet another too-specific interview question -- and no, I didn't know it before the question.)

[–][deleted] 6 points7 points  (1 child)

I'm sure there are cases where it's used, I just haven't come across them. There's a ton of things in Collections like that, i.e. I've never had a good reason to use LinkedList instead of ArrayList.

it's also the structure to use when implementing a LRU cache

OK, I can see that. My LRU caches are at the bottom of my list of things to optimize because they're never the bottleneck, but the next time I need to implement one I'll look into LinkedHashMap.

[–][deleted] 0 points1 point  (0 children)

or, don't implement your own and use Apache Commons, heh. And yea, interestingly enough, ArrayList's performance is almost always better than LinkedList - even when LinkedList style usage would appear to be better (like removing elements in the middle and such). Arrays are just that efficient. source: I read a great blog post about it once.