Ali_S Ali_S - 7 months ago 25
Java Question

compare objects in priority queue

I implemented a priority queue with linked list like this,when for first time I make an object from seller class and add it to priority queue, it works, but when I make second object from seller to
add it to priority queue,it gives an error, I know that my comparator makes this error, but I don't
know, how can I compare objects, please help me!

import java.util.Comparator;

public class PQueueTest {

public static void main(String [] args) {

DefaultComparator<Seller> o = new DefaultComparator<>();
Seller s = new Seller("ali", 125, 200);
Seller s1 = new Seller("hasan", 50, 100);
PriorityQueue<Seller> p = new PriorityQueue<>(o);

p.add(s);
p.add(s1);

System.out.println(p.removeMin());
System.out.println(p.removeMin());
}

}

class Node<E> {

private E element;
private Node<E> next;

public Node(E element, Node<E> next) {
this.element = element;
this.next = next;
}

public void setNext(Node<E> next) {
this.next = next;
}

public void setElement(E element) {
this.element = element;
}

public Node<E> getNext() {
return next;
}

public E getElement() {
return element;
}
}

class DefaultComparator<E> implements Comparator<E> {

@Override
public int compare(E a, E b) {
return ((Comparable<E>) a).compareTo(b);
}
}

class PriorityQueue<E> {

private int size;
private Node<E> front;
private DefaultComparator<E> c;

public PriorityQueue(Comparator<? super E> o) {
size = 0;
front = null;
c = (DefaultComparator<E>) o;

}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

public void add(E element) {
Node<E> v = new Node<>(element, null);
if (isEmpty()) {
front = v;
}

if (size >= 1) {
Node<E> temp = front;

int comp = c.compare(element, temp.getElement());
while (comp >= 0 && temp.getNext() != null) {
temp = temp.getNext();
comp = c.compare(element, temp.getElement());
}

if (comp < 0) {
// E x = temp.getElement();
v.setNext(temp);
if (temp == front)
front = v;
else {
Node<E> tmp = front;
while (tmp.getNext() != temp) {
tmp = tmp.getNext();
}
tmp.setNext(v);
}
}

if (comp >= 0)
temp.setNext(v);
}
size++;
}

public E removeMin() {
E remove = front.getElement();
front = front.getNext();
size--;
return remove;
}

public E removeMax() {
Node<E> tmp = front;
while (tmp.getNext().getNext() != null) {
tmp = tmp.getNext();
}
E remove = tmp.getNext().getElement();
tmp.setNext(null);
size--;
return remove;

}

public E peekMin() {
E remove = front.getElement();
return remove;
}

public E peekMax() {
Node<E> tmp = front;
while (tmp.getNext().getNext() != null) {
tmp = tmp.getNext();
}
E remove = tmp.getNext().getElement();
return remove;
}

}

class Seller {

private String name;
private long price;
private int stock;

public Seller(String name, long price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}

public void setName(String name) {
this.name = name;
}

public void setPrice(long price) {
this.price = price;
}

public void setStock(int stock) {
this.stock = stock;
}

public String getName() {
return name;
}

public long getPrice() {
return price;
}

public int getStock() {
return stock;
}

}

class Buyer {

private String name;
private long price;
private int stock;

public Buyer(String name, long price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}

public void setName(String name) {
this.name = name;
}

public void setPrice(long price) {
this.price = price;
}

public void setStock(int stock) {
this.stock = stock;
}

public String getName() {
return name;
}

public long getPrice() {
return price;
}

public int getStock() {
return stock;
}

}

Answer

First of all: The error is that the cast to Comparable fails. I.e., this line of code:

return ((Comparable<E>)a).compareTo(b);

The reason for that is, that a (in your case, an instance of Seller) does not implement the Comparable interface. You want to compare instances of Seller, so Seller needs to implement the interface (and the corresponding methods):

class  Seller implements Comparable<Seller> {

However you should be aware that Comparator and Comparable are usually two opposing concepts. While the implementation of Comparable allows instances of a class to compare themselves with other instances of that class, with a Comparator this comparison is encapsuled inside another class. Combining those concept usually makes no sense.

So you should

  • Understand what, why and how you should compare
  • Decide on one of the two concepts (Comparable or Comparator). Googling this should bring up good results.
  • If it's the Comparator, assert that only those types can be passed to your Comparator that can be handled by it (hint: take a look at the generic parameter)

And last but not least: Take a look at the warnings your IDE gives you about type safety. Understand them and you will find the major flaw in your code.