Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update:</strong> My previous answer was wrong. You cannot do it without modifying the default behaviour! See below why. </p> <hr> <blockquote> <p><strong>..how to get the last entry or key entered?</strong></p> </blockquote> <p>From the API description of <code>LinkedHashMap</code> you can read:</p> <blockquote> <p>A structural modification is any operation that adds or deletes one or more mappings or, in the case of access-ordered linked hash maps, affects iteration order. In <strong><em>insertion-ordered</strong> linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification</em>. In <strong><em>access-ordered</strong> linked hash maps, merely querying the map with get is a structural modification</em>.</p> </blockquote> <p>So what does it all mean?</p> <ul> <li><em><strong>access-ordered</em></strong> - every time you do a <code>put</code> or a <code>get</code> the order of the elements changes</li> <li><em><strong>insertion-ordered</em></strong> - when inserting elements (for the first time) they are added last</li> </ul> <p>For example:</p> <pre><code>map.put(1, 1); map.put(2, 2); map.put(1, 10); System.out.println(map); </code></pre> <p>... will print <code>{1=10, 2=2}</code> with <em>insertion-ordered</em> and <code>{2=2, 1=10}</code> with *access-ordered'. The trouble is using <code>access-ordered</code> if of course if you do a <code>get</code> operations the order also changes.</p> <hr> <h2>How to fix</h2> <p>So... how to fix. Well the <code>LinkedHashMap</code> cannot be used directly used. So you can wrap it (do not care about the corny name) and override the <code>put</code> and the <code>putAll</code> methods so that they remove the key from the map first before putting it back in!</p> <pre><code>class BestLinkedHashMap&lt;K, V&gt; extends LinkedHashMap&lt;K, V&gt; { @Override public V put(K key, V value) { V last = super.remove(key); super.put(key, value); return last; } @Override public void putAll(Map&lt;? extends K, ? extends V&gt; m) { for (K key : m.keySet()) super.remove(key); super.putAll(m); } } </code></pre> <hr> <p>Then to get the last element, either do:</p> <ul> <li><p>wrap the output from in a <code>LinkedList</code> implementation:</p> <pre><code>V v = new LinkedList&lt;V&gt;(map.values()).getLast(); </code></pre></li> <li><p><code>toArray()</code> way:</p> <pre><code>Collection&lt;V&gt; values = map.values(); V v = values.toArray(new V[0])[values.size() - 1]; </code></pre></li> <li><p>iterate to the last element using the iterator:</p> <pre><code>Iterator&lt;V&gt; it = values.iterator(); V last = null; while (it.hasNext()) last = it.next(); </code></pre></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload