0

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!

bestgio
  • 47
  • 7

4 Answers4

2

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

Collections.sort(theList);
Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • That was just a quick answer to unstuck you. For detailed explanations about appropriate use of `Comparable` & `Comparator` there are already a ton of existing Q&As, for example see [that one](http://stackoverflow.com/a/4108764/6730571) – Hugues M. May 13 '17 at 08:59
1

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);
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    That's a useful complement. Minor complement to that: usually comparators are stateless so it's safe to share a single instance, but it's on the implementor to guarantee that, it's not always the case. So, for a generic answer I'd show how to create a new instance. – Hugues M. May 13 '17 at 08:43
  • I guess you mean `Shape` instead of `AbstractShape`? – NickL May 13 '17 at 08:44
0

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.

dehasi
  • 2,644
  • 1
  • 19
  • 31
0

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());
davidxxx
  • 125,838
  • 23
  • 214
  • 215