bestgio
bestgio

Reputation: 47

Specifying correct comparator when using it in a concrete subclass while compare method is in an abstract class?

I'm having trouble with this one, while the code is really long I'll present a basic view of the hierarchy of all my classes.

Interface:

public interface Shape extends Comparable<Shape> {

Abstract class:

public abstract class AbstractShape implements Shape {
...
public int compareTo(Shape theOther) {
//code already written

public int compare(Shape s1, Shape s2) {
//code already written

Circle class

public class Circle extends AbstractShape {

Rectangle class

public class Rectangle extends AbstractShape {

And finally.. the driver class

public class project {
...
Collections.sort(theList, ???);

After creating a List of all the shapes I have in mind, I would like to sort them using the Collections.sort method. I know how to use simple operations of the Collections.sort method, but how exactly would I specify the statement above to use the specified comparator that's sitting in the abstract class? I thought about changing the hierarchy around but I'm restricted to this hierarchy set up as my instructor does not allow us to modify the method headers at all for this assignment.

Any help is greatly appreciated!

Upvotes: 0

Views: 313

Answers (4)

davidxxx
davidxxx

Reputation: 131346

You don't use a Comparator but you make your class implement the Comparable interface. It is a different thing.

You need only public int compareTo(Shape shape); method. Why do you declare also public int compare(Shape s1, Shape s2) in the AbstractClass.
It makes no sense.

As @Hugues Moreau suggested, you don't need to specify anything as you use Comparable instances that is an alternative to the Comparator use.

After creating a List of all the shapes I have in mind, I would like to sort them using the Collections.sort method

know how to use simple operations of the Collections.sort method, but how exactly would I specify the statement above to use the specified comparator that's sitting in the abstract class? I

You should not compare pigs and monkeys in the sort() method.
The overriden compareTo() method will be applied at runtime.
As you want to use a specific Comparator and not the Comparable implementation that may differ between subclasses, sort them with a Comparator.
For example :

Collections.sort(theList, new MyAbstractShapeComparator());

Upvotes: 0

dehasi
dehasi

Reputation: 2773

You can pass your own comparator to the sort function:

Collections.sort(theList, (o1, o2) -> {
/*compare code*/ 
});

Hugues Moreau is right: you don't need comparator if your class already implements Comparable.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140457

The other answer is correct; but when you intend to use a Comparator, then you simple have to make it possible to provide an instance of that Comparator to the sort method.

In other words: instead of having AbstractShape implement Comparable, you would simply create something like

public class ShapeComparator implements Comparator<AbstractShape>

and then you have to pass some ShapeComparator object to the sort() call. You could even make that thing an enum with a single instance, then you do something like

sort(listOfShapes, ShapeComparator.INSTANCE);

Upvotes: 1

Hugues M.
Hugues M.

Reputation: 20467

Your class implements Comparable so you don't need to pass a Comparator object as second argument, just pass nothing:

Collections.sort(theList);

Upvotes: 2

Related Questions