Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - List sorting doesn't work
    primarykey
    data
    text
    <p>I'm trying to sort a hashmap's by sorting it's keys but it doesn't work. The sorting criteria is given by the length of a list that is the hashmap's value. See code below with some unit test.</p> <p>Class:</p> <pre><code>package com.fabri.interpreter.util; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import com.fabri.interpreter.VerbExpr; import com.fabri.interpreter.ObjectExpr; public class Environment { private HashMap&lt;VerbExpr, List&lt;ObjectExpr&gt;&gt; map = new HashMap&lt;VerbExpr, List&lt;ObjectExpr&gt;&gt;(); public List&lt;ObjectExpr&gt; eval(VerbExpr verb) { return map.get(verb); } public void put(VerbExpr verb, ObjectExpr words) { List&lt;ObjectExpr&gt; values; if(map.get(verb) == null) values = new ArrayList&lt;ObjectExpr&gt;(); else values = map.get(verb); values.add(words); map.put(verb, values); } public HashMap&lt;VerbExpr, List&lt;ObjectExpr&gt;&gt; getMap() { return map; } public void sort() { List&lt;VerbExpr&gt; keys = new ArrayList&lt;VerbExpr&gt;(map.keySet()); Collections.sort(keys, new Comparator&lt;VerbExpr&gt;() { @Override public int compare(VerbExpr verb1, VerbExpr verb2) { return map.get(verb1).size()-map.get(verb2).size(); } }); HashMap&lt;VerbExpr, List&lt;ObjectExpr&gt;&gt; sortedMap = new HashMap&lt;VerbExpr, List&lt;ObjectExpr&gt;&gt;(); for(VerbExpr verb : keys) { sortedMap.put(verb, map.get(verb)); } map = sortedMap; } } </code></pre> <p>Testing class:</p> <pre><code>package com.fabri.interpreter.util; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import com.fabri.interpreter.ObjectExpr; import com.fabri.interpreter.VerbExpr; import com.fabri.interpreter.WordExpr; public class TestEnvironment { private Object[] verbExprs; @Before public void setUp() { Environment env = new Environment(); List&lt;WordExpr&gt; words1 = new ArrayList&lt;WordExpr&gt;(); words1.add(new WordExpr("american")); words1.add(new WordExpr("italian")); env.put(new VerbExpr("was"), new ObjectExpr(words1)); List&lt;WordExpr&gt; words2 = new ArrayList&lt;WordExpr&gt;(); words2.add(new WordExpr("zero")); words2.add(new WordExpr("one")); words2.add(new WordExpr("two")); env.put(new VerbExpr("is"), new ObjectExpr(words2)); env.sort(); verbExprs = env.getMap().keySet().toArray(); } @Test public void testEnvironment() { assertTrue(((VerbExpr)verbExprs[0]).equals("is")); assertTrue(((VerbExpr)verbExprs[1]).equals("was")); } } </code></pre>
    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. 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