title: Java - Inline definition of comparator for Collection.max
body: <p>I am looking for a Java equivalent for python snippet</p>
  
  <pre><code>max_valued_key = max(str_to_double_map.keys(), key=lambda x: str_to_double_map[x])
  </code></pre>
  
  <p>I want to something standard like Collections.max<br>
  Is there a way to do this with <em>inline definition of Comparator</em> since I don't want to write one more class for every other comparator.</p>
  
  <p>I tried following code unsuccessfully</p>
  
  <pre><code>depScores = foo();
  String dep = Collections.max(depScores.keySet(), new Comparator&lt;String&gt;() {
    @Override
    public int compare(String o1, String o2) {
      return depScores.get(o1).compareTo(depScores.get(o2));
    }
  });
  </code></pre>
  
  <p>depScores variable is not readable from comparator.<br>
  Looks like in java inner class cannot access non-final variable from outside!</p>
  
  <p>Thanks in advance!</p>