Search code examples
javacomparator

The method compare(MedicalShifts, MedicalShifts) in the type Comparator<MedicalShifts> is not applicable for the arguments (P, P)


I am trying to use a comparator on a generic array but it gives me the following error

The method compare(MedicalShifts, MedicalShifts) in the type Comparator is not applicable for the arguments (P, P)Java(67108979)

Main.java

public class Main {
    public static void main(String[] args) throws EmptyQueueException {
        Queue<MedicalShifts> queueHospital = new QueueImplement<>();
        queueHospital.add(new MedicalShifts("harry", 0));
        queueHospital.add(new MedicalShifts("hermione", 3));
        queueHospital.add(new MedicalShifts("ron", 1));
        queueHospital.add(new MedicalShifts("luna", 5));
        queueHospital.add(new MedicalShifts("voldemort", 8));
        System.out.println(queueHospital.peek());
        System.out.println(queueHospital.isEmpty());
        try {
            System.out.println(queueHospital.pop());
        } catch (EmptyQueueException emptyQueue) {
            System.out.println(emptyQueue.getMessage());
        }
        System.out.println(queueHospital.peek());
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        queueHospital.add(new MedicalShifts("hagrid", 4));
        for (MedicalShifts shifts : queueHospital) {
            System.out.println(shifts);
        }
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        queueHospital.sort();
        for (MedicalShifts shifts : queueHospital) {
            System.out.println(shifts);
        }
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        System.out.println(queueHospital.min(new Comparator<MedicalShifts>() {
            @Override
            public int compare(MedicalShifts o1, MedicalShifts o2) {
                return o1.getNombre().compareTo(o2.getNombre());
            }
        }));
    }
}

QueueImplement.java

public class QueueImplement<P extends Comparable<P>> implements Queue<P> {
  private P[] elements;
  private static int INITIAL_DIM = 10;
  private int cantidad = 0;

  public QueueImplement() {
    P[] elements = (P[]) new Comparable[INITIAL_DIM]; // Creación de un array de elementos genéricos comparables
    this.elements = elements;
  }

  @Override
  public boolean add(P persona) throws EmptyQueueException {
    if (cantidad == elements.length) {
      throw new EmptyQueueException();
    }
    elements[cantidad++] = persona;
    return true;
  }

  @Override
  public P peek() {
    return elements[0];
  }

  @Override
  public P pop() throws EmptyQueueException {
    P first = elements[0];
    this.elements = Arrays.copyOfRange(elements, 1, elements.length); // si hay error, es - 1
    cantidad--;
    return first;
  }

  @Override
  public P min(Comparator<MedicalShifts> comp) {
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (comp.compare(elements[j], elements[j+1]) > 0) {
                  P temp = elements[j];
                  elements[j] = elements[j+1];
                  elements[j+1] = temp;
             }
         }
    }
    return elements[0];
  }

  @Override
  public P max() { // devuelve el nombre mas grandajo. Se puede sortear y dsp agarrar el ultimo
    return elements[cantidad - 1];
  }

  @Override
  public void sort() {
    Arrays.sort(elements, 0, cantidad);
  }

  @Override
  public Iterator<P> iterator() {
    return new Iterator<P>() {
      private int index = 0;

      @Override
      public boolean hasNext() {
        return index < cantidad;
      }

      @Override
      public P next() {
        return elements[index++];
      }
    };
  }

  @Override
  public boolean isEmpty() {
    return cantidad == 0;
  }
}

The error happens on the comp.compare in the min function. All the other methods work perfectly with generics, so I don´t know why it is not working with the compare function that the comparator has.


Solution

  • You've mixed the generic and specific type. Your min should be generic on P (not specific on MedicalShifts). You wanted

    @Override
    public P min(Comparator<P> comp) {
        for (int i = 0; i < n-1; i++){
            for (int j = 0; j < n-i-1; j++){
                if (comp.compare(elements[j], elements[j+1]) > 0) {
                    P temp = elements[j];
                    elements[j] = elements[j+1];
                    elements[j+1] = temp;
                }
            }
        }
        return elements[0];
    }
    

    I don't know about sorting in a min function. Maybe

    @Override
    public P min(Comparator<P> comp) {
        P m = null;
        for (int i = 0; i < n; i++){
            if (m == null || comp.compare(m, elements[i]) < 0) {
                m = elements[i];
            }
        }
        return m;
    }