Csbk
Csbk

Reputation: 49

sorting a list of pair of integers in ascending order in java?

I am using the javafx pair class and trying to sort them using the second value.

  public static void main(String[] args)
   {
    List<Pair<Integer, Integer>> dist = new ArrayList<Pair<Integer, Integer>>();
    dist.add(new Pair <Integer,Integer> (4,54));
    dist.add(new Pair <Integer,Integer> (6,94));
    dist.add(new Pair <Integer,Integer> (7,4));
    dist.add(new Pair <Integer,Integer> (11,9));

    Collections.sort(dist, new ListComparator<>());

    System.out.println(dist);

     }

 public class ListComparator implements Comparator {

 @Override
public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
        return o1.getValue() - o2.getValue();

    }
  }

i keep getting the following errors and warning.

/tmp/java_izg9ZN/Main.java:34: error: cannot infer type arguments for ListComparator Collections.sort(dist, new ListComparator<>());
reason: cannot use '<>' with non-generic class ListComparator

/tmp/java_izg9ZN/ListComparator.java:21: error: ListComparator is not abstract and does not override abstract method compare(Object,Object) in Comparator public class ListComparator implements Comparator {

/tmp/java_izg9ZN/ListComparator.java:23: error: method does not override or implement a method from a supertype @Override

i have no idea what i am doing wrong because i example i looked at to do this did exactly the same thing so i really dont know what i have done wrong. please help! thanks

Upvotes: 1

Views: 1426

Answers (1)

Zabuzard
Zabuzard

Reputation: 25903

Your code has two problems. First you call:

Collections.sort(dist, new ListComparator<>());

So you tell Java to use a ListComparator which infers the type by itself (diamond operator <>).

However your ListComparator class is not generic. It does not expect any type arguments, like a String for example, you don't write String<...>.

You could fix this by making ListComparator generic, working on the elements of the List for example:

public class ListComparator<E> implements Comparator<E> {
    // Compare E elements ...
}

However it seems that you rather want a non-generic ListComparator which is fixed to comparing Pair<Integer, Integer> elements. In that case remove the diamond operator:

Collections.sort(dist, new ListComparator());

The second problem is that you use raw-types for the Comparator instead of telling him the type of the objects that will be compared.

Because of that the compiler expects at reading @Override that your compareTo methods matches the signature written in Comparator:

public int compareTo(Object o1, Object o2)

And not

public int compareTo(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2)

You fix this by explicitly telling Comparator what to use instead of using raw-types:

public class ListComparator implements Comparator<Pair<Integer, Integer>> {
    // Compare Pair<Integer, Integer> elements ...
}

Upvotes: 4

Related Questions