Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think you'll get more compact than the three lines above, not in Java.</p> <p>Apart from that, I have the impression that a <code>Map</code> as a data structure is the wrong choice in the first place, since you do not seem to need by-string lookups (UNLESS you want in some way deal with multiple occurences of strings, but you didn't say so). An alternative approach would be to declare your own comparable data record class:</p> <pre><code>private static class Record implements Comparable&lt;Record&gt; { // public final fields ok for this small example public final String string; public final double value; public Record(String string, double value) { this.string = string; this.value = value; } @Override public int compareTo(Record other) { // define sorting according to double fields return Double.compare(value, other.value); } } // provide size to avoid reallocations List&lt;Record&gt; records = new ArrayList&lt;Record&gt;(stringList.size()); for(String s : stringList) records.add(new Record(s, calculateFitness(s)); Collections.sort(records); // sort according to compareTo method int max = Math.min(10, records.size()); // maximum index List&lt;String&gt; result = new ArrayList&lt;String&gt;(max); for(int i = 0; i &lt; max; i++) result.add(records.get(i).string); return result; </code></pre> <p>This is now much more verbose than the three lines above (this is Java, after all), but also includes the code that would be required to insert the key/value pairs into the map.</p>
    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.
 

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