1

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 to Pair<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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tony
  • 1,127
  • 1
  • 18
  • 29
  • I do understand what raw types are (at least in general) but I do not understand how I am not type checking here. – Tony Jun 08 '16 at 14:26
  • 3
    You're using `Comparator` when you should be using `Comparator` – Yassin Hajaj Jun 08 '16 at 14:28
  • Please, can you give some example of data before and after ordering. – bilelovitch Jun 08 '16 at 14:30
  • Yes, but I have to write something, because there are too much data where I am reading from right now. Give me a bit of time. Thanks – Tony Jun 09 '16 at 14:24
  • I just took the first 10 from each. There are like 45000 records so you won't see the same ones, just that it is sorted by date. Also, these comments don't let you format correctly so I apologize in advance: BEFORE 1021639 : 2004-09-10 14:31:20.377 1992434 : 2008-04-09 15:30:29.36 3042521 : 2010-03-24 16:54:26.047 640574 : 2002-12-13 17:12:59.18 680149 : 2003-02-06 10:32:45.993 90468 : 1999-10-07 18:05:40.0 1282319 : 2005-10-25 13:30:30.51 959208 : 2004-06-03 09:21:46.26 2418554 : 2008-12-03 15:38:59.7 2545436 : 2009-03-02 11:49:42.47 – Tony Jun 09 '16 at 15:06
  • AFTER 297051 : 1998-05-07 11:30:13.0 297050 : 1998-05-07 11:30:13.0 64105 : 1998-05-07 11:31:21.0 142719 : 1998-05-07 11:31:34.0 297046 : 1998-05-07 11:31:34.0 241291 : 1998-05-07 11:31:34.0 267941 : 1998-05-07 11:31:34.0 297042 : 1998-05-07 11:31:34.0 229681 : 1998-05-07 11:31:34.0 229898 : 1998-05-07 11:31:34.0 – Tony Jun 09 '16 at 15:06

2 Answers2

0

Just use this

Comparator<Pair<String, Date>> mycomp = new Comparator<Pair<String, Date>>(){

And you won't need to cast anymore as your compare method will be

 public int compare(Pair<String, Date> o1, Pair<String, Date> o2) 
Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69
0

Specialize your comparator to Pair

java.util.Comparator mycomp = new java.util.Comparator<Pair<Integer, Date>>() {
    @Override
        public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
           // compare here.
       }
};

Pair could be used in many places in your application, So you could also do it as a separate class and reuse the objects elsewhere.

public class MyPairComparator implements Comparator<Integer,Date> {
    public int compare(Pair<Integer, Date> pair1, Pair<Integer, Date> pair2) {
        // compare here.
       }
}

// To Use
Collections.sort( pairs, new MyPairComparator());
Sam Daniel
  • 1,800
  • 12
  • 22