Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28135
| From | Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: LinkedHashMap - get latest key? |
| Date | 2014-04-19 12:51 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnll4sb5.bi9.avl@login.logic.tuwien.ac.at> (permalink) |
| References | <slrnljo8ia.rhr.avl@login.logic.tuwien.ac.at> <T4qdnaNtY8U7DNLOnZ2dnUVZ8radnZ2d@giganews.com> |
Leif Roar Moldskred <leif.roar@invalid.com> wrote: > Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> wrote: >> Given a LinkedHashMap instance, what would be a >> reasonable way to obtain the latest added key? >> The specific definition of "reasonable" being: >> - no separate keeping track of key-sequence >> - it should be a O(1) operation, and not >> have to iterate all keys. > If you don't have to remove objects from the map, or you can live with > remove being O( n ) when it's the latest added element that's removed, > you can just subclass LinkedHashMap like below. (Warning: the code > hasn't been tested, just written. Caveat emptor.) Most of the operations (add,remove,find&raise) on that structure will be dwarfed by other stuff happening alongside, except for "accessing the top-object", for which ideally no CPU-cycle should be wasted at all. Based on this requirement, I already dumped the Hashing. Size "n" is not very large in my case (probably below 10 most of the times), and linearly searching an element (typically to raise it to top) just doesn't matter here. The same short linear search for the last element, however, did matter. Thanks anyway for your code contribution. It would have solved my problem, but my code (without hashing) became simpler, which is now even preferrable to search-speed for me.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
LinkedHashMap - get latest key? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-04-02 14:44 +0000
Re: LinkedHashMap - get latest key? Jeff Higgins <jeff@invalid.invalid> - 2014-04-02 12:06 -0400
Re: LinkedHashMap - get latest key? Jeff Higgins <jeff@invalid.invalid> - 2014-04-02 12:44 -0400
Re: LinkedHashMap - get latest key? Jeff Higgins <jeff@invalid.invalid> - 2014-04-02 13:13 -0400
Re: LinkedHashMap - get latest key? Jeff Higgins <jeff@invalid.invalid> - 2014-04-02 13:19 -0400
Re: LinkedHashMap - get latest key? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-04-02 21:58 +0000
Re: LinkedHashMap - get latest key? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2014-04-16 21:33 -0700
Re: LinkedHashMap - get latest key? leif.roar@invalid.com (Leif Roar Moldskred) - 2014-04-17 03:32 -0500
Re: LinkedHashMap - get latest key? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-04-19 12:51 +0000
Re: LinkedHashMap - get latest key? v_borchert@despammed.com (Volker Borchert) - 2014-04-21 20:38 +0000
Re: LinkedHashMap - get latest key? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-04-25 09:18 +0000
csiph-web