【问题标题】:Java CompareTo for objects not WorkingJava CompareTo 对象不工作
【发布时间】:2013-12-10 05:06:22
【问题描述】:
public class Book implements Comparable<Book>, Serializable{
int numberOfPages;
boolean hardback;
String title;
double price;

public Book (int numberOfPages,boolean hardback, String title,double price)throws          BookException {
    setNumberOfPages(numberOfPages);
    setHardback(hardback);
    setPrice(price);
    setTitle(title);
}
public int getNumberOfPages(){
    return numberOfPages;
}
public void setNumberOfPages(int numberOfPages) throws BookException {
    if(numberOfPages < 1){
        throw new BookException("Pages must be a positive integer");
    }
    this.numberOfPages = numberOfPages;
}
public boolean isHardback(){
    return hardback;
}
public void setHardback(boolean hardback) {
    this.hardback = hardback;
}
public String getTitle(){
       return title;
}
public void setTitle(String title) throws BookException {
    if (title == null || title.isEmpty()){
      throw new BookException("No title must be less than 0 characters");
    }
    this.title = title;
}
public double getPrice(){
    return price;
}
public void setPrice(double price) throws BookException {
    if (price < 0){
        throw new BookException("Price can't be negative");
    }
    this.price = price;
}
@Override
public int compareTo(Book o){
    int Before =  -1;
    int Equal =  0;
    int After =  1;
    if (this == o) return Equal;
    if (this.getNumberOfPages() < o.getNumberOfPages()) return Before;
    if (this.getNumberOfPages() > o.getNumberOfPages()) return After;
    if (this.getPrice() < o.getPrice()) return After;
    if (this.getPrice() > o.getPrice()) return Before;
    int comparison = this.getTitle().compareTo(o.getTitle());
    if (comparison != Equal) return comparison;
    return Equal;
}
@Override public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Book)) return false;

    Book that = (Book)o;
    return
            ( this.numberOfPages == that.numberOfPages ) &&
                    ( this.price == that.price ) &&
                    ( this.title.equals(that.title) )
            ;
}
@Override
public String toString(){
    StringBuilder sb = new StringBuilder();
        sb.append("Title " + getTitle());
        sb.append("\n");
        sb.append("Number Of Pages " + getNumberOfPages());
        sb.append("\n");
        sb.append("Is Hardback " + isHardback());
        sb.append("\n");
        sb.append("Price " + getPrice());
        sb.append("\n");
        return sb.toString();
    }
 }

当我运行此代码时,订单全乱了,好像 compareTo 不起作用。

import java.util.Iterator;
import java.util.PriorityQueue;
public class BookQueue {
public static void main(String[] args)throws BookException  {

        PriorityQueue<Book> pq = new PriorityQueue<Book>();
        pq.offer(new Book(22, true,"Cat in the hat", 10));
        pq.offer(new Book(32, true,"Green eggs and ham", 12));
        pq.offer(new Book(42, true,"Hop on Pop", 11));
        pq.offer(new Book(20, true,"Lorax", 19));
        pq.offer(new Book(20, false,"Lorax", 19));

        Iterator itr = pq.iterator();
        System.out.println("\n");
        while(itr.hasNext()){
           System.out.println(itr.next());
        }


}

}

这是输出:

它应该按最少的页数排序,然后是价格,然后是标题 它从 20 开始,然后到 42,然后是 32 和 22?

Title Lorax
Number Of Pages **20**
Is Hardback true
Price 19.0

Title Lorax
Number Of Pages **20**
Is Hardback false
Price 19.0

Title Hop on Pop
Number Of Pages **42**
Is Hardback true
Price 11.0

Title Green eggs and ham
Number Of Pages **32**
Is Hardback true
Price 12.0

Title Cat in the hat
Number Of Pages **22**
Is Hardback true
Price 10.0

【问题讨论】:

    标签: java compare equals priority-queue compareto


    【解决方案1】:

    使用这个compareTo() 方法。

    @Override
    public int compareTo(Book o){
        if (this.getNumberOfPages() != o.getNumberOfPages()) 
             return   new Integer(this.getNumberOfPages()).compareTo(o.getNumberOfPages());    
        if (this.getPrice() != o.getPrice()) 
            return   new Double(this.getPrice()).compareTo(o.getPrice());    
        if(!this.getTitle().equalsIgnoreCase(o.getTitle()))
            return this.getTitle().compareToIgnoreCase(o.getTitle());
        return 0;
    }
    

    【讨论】:

    • 问题不在于campareTo,而在于PriorityQueue的工作方式
    【解决方案2】:

    如果使用iterator()方法,PriorityQueue类不保证顺序。

    Javadoc 明确将其定义为 here

    方法 iterator() 中提供的迭代器不能保证以任何特定顺序遍历优先级队列的元素。如果需要有序遍历,可以考虑使用 Arrays.sort(pq.toArray())。

    你只需要修改main如下。

    import java.util.Arrays;
    import java.util.PriorityQueue;
    
    public class BookQueue {
        public static void main(String[] args) throws BookException {
    
            PriorityQueue<Book> pq = new PriorityQueue<Book>();
            pq.offer(new Book(32, true, "Green eggs and ham", 12));
            pq.offer(new Book(42, true, "Hop on Pop", 11));
            pq.offer(new Book(20, true, "Lorax", 19));
            pq.offer(new Book(20, false, "Lorax", 19));
            pq.offer(new Book(22, true, "Cat in the hat", 10));
    
            // Iterator itr = pq.iterator();
            // System.out.println("\n");
            // while(itr.hasNext()){
            //   System.out.println(itr.next());
            //}
    
            Book[] books = new Book[pq.size()];
            Arrays.sort(pq.toArray(books));
            System.out.println("\n");
            for (int i = 0; i < books.length; i++) {
                System.out.println(books[i]);
            }
        }
    }
    

    希望这会有所帮助。如果您需要Comparator vs Comparable 中详述的不同排序方法,您也可以尝试实现Comparator 并传递到Arrays.sort(T[], Comparator)

    【讨论】:

    • 没想过这样做,但我尝试实现比较器并且它有效
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多