title: How to retrieve the key with a maximum value in a TreeMap in Java?
body: <p>I have a tree map declared as follows:</p>
  
  <pre><code>TreeMap&lt;Integer, Integer&gt; tree = new TreeMap&lt;Integer, Integer&gt;();
  </code></pre>
  
  <p>How do I retrieve the key with the maximum value. Is there an O(1) way of achieving this. I know that maximum and minimum keys can be retrieved from a TreeMap in O(1) time as follows:</p>
  
  <pre><code>int maxKey = tree.lastEntry().getKey();
  int minKey = tree.firstEntry().getKey();
  </code></pre>
  
  <p>Thanks for help.</p>