Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused how to type comparator in another class
    primarykey
    data
    text
    <p>I am trying to sort the values of two LinkedHashMap. I can compile it and run the code just fine, but it tells me to use -Xlint option during compiling because it is unsafe code. It has something to do with type casting stuff, but I am royally confused on how to do it. I got this class which I put inbedded in my class:</p> <pre><code>static class MyComparator implements Comparator { public int compare(Object obj1, Object obj2){ int result=0; Map.Entry e1 = (Map.Entry)obj1 ; Map.Entry e2 = (Map.Entry)obj2 ;//Sort based on values. Integer value1 = (Integer)e1.getValue(); Integer value2 = (Integer)e2.getValue(); if(value1.compareTo(value2)==0){ String word1=(String)e1.getKey(); String word2=(String)e2.getKey(); //Sort String in an alphabetical order result=word1.compareToIgnoreCase(word2); } else { //Sort values in a descending order result=value2.compareTo( value1 ); } return result; } } </code></pre> <p>I tried to call it in one of my functions with:</p> <pre><code>ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet()); Collections.sort(myArrayList, new MyComparator()); Iterator itr=myArrayList.iterator(); </code></pre> <p>Note: this.map_freq_by_date is defined as follows:</p> <pre><code>Map&lt;String,Integer&gt; map_freq_by_date = new LinkedHashMap&lt;String,Integer&gt;(); </code></pre> <p>The error I get with -Xlint option:</p> <pre><code>unchecked call to ArrayList(java.util.Collection&lt;? extends E&gt;) as a member of the raw type java.util.ArrayList ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet()); unchecked conversion found LogGrep.MyComparator required: java.util.Comparator(? super T&gt; Collections.sort(myArrayList, new MyComparator()); unchecked method invocation: &lt;T&gt;sort(java.util.List&lt;T&gt;,java.util.Comparator&lt;? super T&gt; in java.util.Collections is applied to (java.util.ArrayList,LogGrep.MyComparator) Collections.sort(myArrayList, new MyComparator()); </code></pre> <p>Help with how to fix these would be appreciated. I looked online and tried all kinds of things shown, but I can't seem to get it right.</p> <p>Note: if I put <code>ArrayList&lt;Object&gt; myArrayList = new ArrayList&lt;Object&gt;</code>... the error changes to:</p> <pre><code>unchecked method invocation &lt;T&gt;sort(java.util.List&lt;T&gt;,java.util.Comparator&lt;&gt; super T?) in java.util.Collections is applied ot (java.util.ArraList&lt;java.lang.Object&gt;,LogGrep.MyComparator) Collections.sort(myArrayList, new MyComparator()); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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