Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer your question in one sentence:</p> <p><strong>Per default, Maps don't have a last entry, it's not part of their contract.</strong></p> <hr> <p>And a side note: it's good practice to code against interfaces, not the implementation classes (see <a href="http://java.sun.com/docs/books/effective/" rel="noreferrer">Effective Java by Joshua Bloch</a>, Chapter 8, Item 52: <strong>Refer to objects by their interfaces</strong>).</p> <p>So your declaration should read:</p> <pre><code>Map&lt;String,Integer&gt; map = new HashMap&lt;String,Integer&gt;(); </code></pre> <p>(All maps share a common contract, so the client need not know what kind of map it is, unless he specifies a sub interface with an extended contract).</p> <hr> <h2>Possible Solutions</h2> <h3>Sorted Maps:</h3> <p>There is a sub interface <a href="http://download.oracle.com/javase/6/docs/api/java/util/SortedMap.html" rel="noreferrer">SortedMap</a> that extends the map interface with order-based lookup methods and it has a sub interface <a href="http://download.oracle.com/javase/6/docs/api/java/util/NavigableMap.html" rel="noreferrer">NavigableMap</a> that extends it even further. The standard implementation of this interface, <a href="http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html" rel="noreferrer">TreeMap</a>, allows you to sort entries either by natural ordering (if they implement the <a href="http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="noreferrer">Comparable</a> interface) or by a supplied <a href="http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="noreferrer">Comparator</a>.</p> <p>You can access the last entry through the <a href="http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html#lastEntry()" rel="noreferrer">lastEntry</a> method:</p> <pre><code>NavigableMap&lt;String,Integer&gt; map = new TreeMap&lt;String, Integer&gt;(); // add some entries Entry&lt;String, Integer&gt; lastEntry = map.lastEntry(); </code></pre> <h3>Linked maps:</h3> <p>There is also the special case of <a href="http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html" rel="noreferrer">LinkedHashMap</a>, a HashMap implementation that stores the order in which keys are inserted. There is however no interface to back up this functionality, nor is there a direct way to access the last key. You can only do it through tricks such as using a List in between:</p> <pre><code>Map&lt;String,String&gt; map = new LinkedHashMap&lt;String, Integer&gt;(); // add some entries List&lt;Entry&lt;String,Integer&gt;&gt; entryList = new ArrayList&lt;Map.Entry&lt;String, Integer&gt;&gt;(map.entrySet()); Entry&lt;String, Integer&gt; lastEntry = entryList.get(entryList.size()-1); </code></pre> <h3>Proper Solution:</h3> <p>Since you don't control the insertion order, you should go with the NavigableMap interface, i.e. you would write a comparator that positions the <code>Not-Specified</code> entry last.</p> <p>Here is an example:</p> <pre><code>final NavigableMap&lt;String,Integer&gt; map = new TreeMap&lt;String, Integer&gt;(new Comparator&lt;String&gt;() { public int compare(final String o1, final String o2) { int result; if("Not-Specified".equals(o1)) { result=1; } else if("Not-Specified".equals(o2)) { result=-1; } else { result =o1.compareTo(o2); } return result; } }); map.put("test", Integer.valueOf(2)); map.put("Not-Specified", Integer.valueOf(1)); map.put("testtest", Integer.valueOf(3)); final Entry&lt;String, Integer&gt; lastEntry = map.lastEntry(); System.out.println("Last key: "+lastEntry.getKey() + ", last value: "+lastEntry.getValue()); </code></pre> <p>Output:</p> <blockquote> <p>Last key: Not-Specified, last value: 1</p> </blockquote> <h3>Solution using HashMap:</h3> <p>If you must rely on HashMaps, there is still a solution, using a) a modified version of the above comparator, b) a <a href="http://download.oracle.com/javase/6/docs/api/java/util/List.html" rel="noreferrer">List</a> initialized with the Map's <a href="http://download.oracle.com/javase/6/docs/api/java/util/Map.html#entrySet()" rel="noreferrer">entrySet</a> and c) the <a href="http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29" rel="noreferrer">Collections.sort()</a> helper method:</p> <pre><code> final Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;(); map.put("test", Integer.valueOf(2)); map.put("Not-Specified", Integer.valueOf(1)); map.put("testtest", Integer.valueOf(3)); final List&lt;Entry&lt;String, Integer&gt;&gt; entries = new ArrayList&lt;Entry&lt;String, Integer&gt;&gt;(map.entrySet()); Collections.sort(entries, new Comparator&lt;Entry&lt;String, Integer&gt;&gt;(){ public int compareKeys(final String o1, final String o2){ int result; if("Not-Specified".equals(o1)){ result = 1; } else if("Not-Specified".equals(o2)){ result = -1; } else{ result = o1.compareTo(o2); } return result; } @Override public int compare(final Entry&lt;String, Integer&gt; o1, final Entry&lt;String, Integer&gt; o2){ return this.compareKeys(o1.getKey(), o2.getKey()); } }); final Entry&lt;String, Integer&gt; lastEntry = entries.get(entries.size() - 1); System.out.println("Last key: " + lastEntry.getKey() + ", last value: " + lastEntry.getValue()); } </code></pre> <p>Output:</p> <blockquote> <p>Last key: Not-Specified, last value: 1</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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