I have an object that contains a list of String and date:
List<Pair<String, Date>> res;
Then I wrote a comparator
Comparator mycomp = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if ((o1.getClass().equals(ImmutablePair.class))
&& (o2.getClass().equals(ImmutablePair.class))) {
Pair<Integer, Date> p1 = (Pair<Integer, Date>) o1;
Pair<Integer, Date> p2 = (Pair<Integer, Date>) o1;
return comPair(p1, p2);
}
throw new AssertionError("Unknown Types");
}
public int comPair(Pair<Integer, Date> p1, Pair<Integer, Date> p2) {
return p1.getValue().compareTo(p2.getValue());
}
};
This works but I get several warnings.
The first line:
Comparator is a raw type. References to generic type
Comparator<T>
should be parameterized.
Casting of p1 and p2:
Type safety: Unchecked cast from
Object
toPair<Integer,Date>
For the casting I thought I was checking the type with Pair<String, Date>
.
As for the declaration, Comparator mycomp = new Comparator()
, I try to put new Comparator(Pair<String, Date>)
I get this:
- Comparator is a raw type. References to generic type
Comparator<T>
should be parameterized - Syntax error on token ">", Expression expected after this token
If I try to put an object name
Comparator mycomp = new Comparator(Pair<String, Date> obj)
I get all sorts of errors that Pair is not found and String is not found, and there is not an option to import them.
So what am I doing wrong?