> Can I safely ignore the warning: Type safety: The expression
> of type TestMapping.CoordinateComparator needs unchecked
> conversion to conform to Comparator<? super String>
No.
Define your CoordinateComparator using generics.
You should never ignore warnings. You can disregard the "unchecked" warning
in specific situations; in fact Java pretty much guarantees that you have to.
For this use @SuppressWarnings( "unchecked" ). Note that this does not
/ignore/ the warning, it most explicitly and in a self-documenting way
suppresses it.

Signature
Lew
> Hi,
> When I run the following code
[quoted text clipped - 11 lines]
>
> Is there something wrong with my CoordinateComparator?
I think so. In the case c[0]==0, c[1]!=0 it always
returns 1. Hence 1:2:3 compares greater than 1:3:4, but
at the same time 1:3:4 compares greater than 1:2:3.
> Do I not understand how TreeMap works?
> Something else?
[quoted text clipped - 3 lines]
> of type TestMapping.CoordinateComparator needs unchecked
> conversion to conform to Comparator<? super String>
Try using Comparator<String> instead of raw Comparator,
and change the types of the compare() arguments from Object
to String.
> Thanks
> Jeff Higgins
[quoted text clipped - 100 lines]
> }
> }
An easier way to write this might be
if (c[0] != 0)
return c[0];
if (c[1] != 0)
return c[1];
return c[2];
... with the added benefit that it's easy to see the
Comparator contract is fulfilled.
> static final int signum ( long diff )
> {
[quoted text clipped - 4 lines]
> }
> }

Signature
Eric Sosman
esosman@acm-dot-org.invalid
Jeff Higgins - 20 May 2007 20:39 GMT
>> Is there something wrong with my CoordinateComparator?
>
[quoted text clipped - 9 lines]
> return c[1];
> return c[2];
how nice :-)
> ... with the added benefit that it's easy to see the
> Comparator contract is fulfilled.
>> Can I safely ignore the warning: Type safety: The expression
>> of type TestMapping.CoordinateComparator needs unchecked
[quoted text clipped - 3 lines]
> and change the types of the compare() arguments from Object
> to String.
Great!
Straightforward help greatly appreciated.
Thanks so much.
Jeff Higgins
static class CoordinateComparator
implements Comparator<String>
{
public final int compare(String s1, String s2)
{// thanks rg es
String[] sa = s1.split(":");
long[] a = new long[3];
a[0] = Long.valueOf(sa[0]);
a[1] = Long.valueOf(sa[1]);
a[2] = Long.valueOf(sa[2]);
String[] sb = s2.split(":");
long[] b = new long[3];
b[0] = Long.valueOf(sb[0]);
b[1] = Long.valueOf(sb[1]);
b[2] = Long.valueOf(sb[2]);
long[] c = new long[3];
c[0] = signum(a[0] - b[0]);
c[1] = signum(a[1] - b[1]);
c[2] = signum(a[2] - b[2]);
if (c[0] != 0)
return (int)c[0];
if (c[1] != 0)
return (int)c[1];
return (int)c[2];
}
}
>> import java.util.ArrayList;
>> import java.util.Comparator;
[quoted text clipped - 104 lines]
>> }
>> }