Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: LinkedHashMap - get latest key? Date: Wed, 02 Apr 2014 12:06:24 -0400 Organization: A noiseless patient Spider Lines: 23 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 2 Apr 2014 16:08:43 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="6b9e92482af47766954d85fb77d372f5"; logging-data="10023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Wj+oMGYg2c+lgyxWqQU6m5HtPH/eyiUA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 In-Reply-To: Cancel-Lock: sha1:AiI8srvpdAmDjgrtcPI/BCVtupE= Xref: csiph.com comp.lang.java.programmer:27937 On 04/02/2014 10:44 AM, Andreas Leitgeb 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. > > Unlike TreeMap, the LinkedHashMap doesn't implement > NavigableMap. > > If nothing else helps, I'll need to switch to a LinkedList, > and thereby sell the "O(1)" of searching for an Object (by > key) in exchange for a "O(1)" of obtaining the last Object. > > It just seems odd to me, that a LinkedHashMap doesn't > have such a nobrainer getter, or, that a LinkedHashMap > isn't also a NavigableMap with the natural ordering > induced by position. > ? Collections.max(myMap.entrySet())