There was a need to get rid of the second compare compare method, however, after its removal, the program is not compiled. Tell me what could be the snag?

 public class Car implements Comparator<ArrayList> { private int speed; public int getSpeed() { return speed; } public Car(int speed) { this.speed = speed; } public static void main(String[] args) { Random rand = new Random(); ArrayList<ArrayList<Car>> listsOfCars = new ArrayList<>(); for (int i = 0; i < 35; i++) { int a = rand.nextInt(50); ArrayList<Car> cars = new ArrayList<Car>(); for (int j = 0; j < a; j++) { cars.add(new Car(a)); } listsOfCars.add(cars); } Collections.sort(listsOfCars, SizeComparator); for (ArrayList<Car> cars : listsOfCars) { System.out.println(cars.size()); } } public static Comparator<ArrayList> SizeComparator = new Comparator<ArrayList>() { @Override public int compare(ArrayList arrlist1, ArrayList arrlist2) { return arrlist1.size() - arrlist2.size(); } }; @Override public int compare(ArrayList arrlist1, ArrayList arrlist2) { return 0; } } 
  • remove implements Comparator <ArrayList> in class declaration - Artem Konovalov
  • @ArtemKonovalov thanks, it works! If not difficult, tell me why it was necessary to delete? - Oleg
  • one
    Because you have determined that a class can compare, but at the same time it cannot do this, because There is no method for comparison. - Artem Konovalov
  • @ArtemKonovalov Thanks again to you! - Oleg
  • @Oleg please - Artem Konovalov

1 answer 1

In the class declaration, you have indicated that it implements the Comparator interface, but at the same time, the class itself does not implement this method.

For the code to compile, you need either

  • Implement this method
  • To declare a class abstract and leave the implementation at the mercy of classes to heirs