比较优先队列中的对象

时间:2014-06-06 01:16:59

标签: java

我实现了一个像这样的链接列表的优先级队列,当我第一次从卖家类创建一个对象并将其添加到优先级队列时,它可以工作,但是当我从卖家到第二个对象时 将它添加到优先级队列,它会出错,我知道我的比较器会出现此错误,但我不知道 知道,我怎么比较对象,请帮助我!

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;
    }

}

1 个答案:

答案 0 :(得分:2)

首先:错误是对Comparable的强制转换失败。即,这行代码:

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

原因是,a(在您的情况下,Seller的实例)未实现Comparable接口。您想要比较Seller的实例,因此Seller需要实现接口(以及相应的方法):

class  Seller implements Comparable<Seller> {

然而你应该知道Comparator和Comparable通常是两个相反的概念。虽然Comparable的实现允许类的实例将自己与该类的其他实例进行比较,但使用Comparator时,此比较将封装在另一个类中。结合这些概念通常是没有意义的。

所以你应该

  • 了解什么,为什么以及如何比较
  • 决定两个概念中的一个(Comparable或Comparator)。谷歌搜索这应该会产生良好的效果。
  • 如果它是比较器,断言只有那些类型可以传递给你的Comparator,它可以由它处理(提示:看看泛型参数)

最后但同样重要的是:看看IDE为您提供的类型安全警告。了解它们,您将发现代码中的主要缺陷。