gpt4 book ai didi

java - 为什么 PriorityQueue 需要 Comparable?

转载 作者:行者123 更新时间:2023-11-29 08:28:27 25 4
gpt4 key购买 nike

我正在学习队列集合,但我不明白为什么如果使用 PriorityQueue 还需要实现 Comparable 接口(interface)?我在数组/列表的自定义排序中使用了 Comparable 和 Comparator 接口(interface)。但是,当我不想对它进行排序时,为什么我需要实现 PriorityQueue...

在下面的示例中,我发现如果我不实现 Comparable 接口(interface),则无法在 PriorityQueue 中添加元素。

对我来说最大的误解是,在我运行这段代码后,列表不是按 id 排序的?我问这个问题是因为我看到compareTo()方法被重写,并且当我想对列表进行排序时我使用这个方法。

我记得当我对列表进行自定义排序时,我使用了 exaclty 这段代码。并且该列表是按 id 排序的。为什么在这种情况下队列没有排序?

抱歉我的英语语法。任何反馈将不胜感激!

import java.util.PriorityQueue;
import java.util.Queue;

public class QueueExample {

public static void main(String[] args) {

Queue<Book> queue = new PriorityQueue<>();

//Creating Books
Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(233,"Operating System","Galvin","Wiley",6);
Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b4=new Book(319,"Learn Java","Yanis Orhan","HEX",3);
Book b5=new Book(191,"Linux","Hadgy","Haman",7);
Book b6=new Book(287,"Python Programming","Tarzan","GEN",5);

//Adding Books to the queue
queue.add(b1);
queue.add(b2);
queue.add(b3);
queue.add(b4);
queue.add(b5);
queue.add(b6);

System.out.println("Traversing the queue elements:");

//Traversing queue elements
for(Book b : queue) {

System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}

queue.remove();
System.out.println("After removing one book record:");
for(Book b : queue) {

System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
}
}

预订

public class Book implements Comparable<Book> {

int id;
String name,author,publisher;
int quantity;

public Book(int id, String name, String author, String publisher, int quantity) {

this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}

@Override
public int compareTo(Book b) {

if(id > b.id) {

return 1;
} else if (id < b.id) {

return -1;
} else {

return 0;
}
}
}

我看到 head 是 id 最低的元素,但之后就没有顺序了。为什么队列不按 id 排序?

Traversing the queue elements:
101 Data Communications & Networking Forouzan Mc Graw Hill 4
191 Linux Hadgy Haman 7
121 Let us C Yashwant Kanetkar BPB 8
319 Learn Java Yanis Orhan HEX 3
233 Operating System Galvin Wiley 6
287 Python Programming Tarzan GEN 5
After removing one book record:
121 Let us C Yashwant Kanetkar BPB 8
191 Linux Hadgy Haman 7
287 Python Programming Tarzan GEN 5
319 Learn Java Yanis Orhan HEX 3
233 Operating System Galvin Wiley 6

更新

如果我创建一个ArrayList,并使用相同的compareTo()方法对其进行排序,并使用相同的for-each循环打印它,则列表将被排序并按此顺序打印。

ArrayList 的代码:

import java.util.ArrayList;
import java.util.Collections;

public class BookExample {

public static void main(String args[]) {

ArrayList<Book> bookList = new ArrayList<>();

Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(233,"Operating System","Galvin","Wiley",6);
Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b4=new Book(319,"Learn Java","Yanis Orhan","HEX",3);
Book b5=new Book(191,"Linux","Hadgy","Haman",7);
Book b6=new Book(287,"Python Programming","Tarzan","GEN",5);

bookList.add(b1);
bookList.add(b2);
bookList.add(b3);
bookList.add(b4);
bookList.add(b5);
bookList.add(b6);

Collections.sort(bookList);

for (Book b : bookList) {

System.out.println(b.id + ", " + b.name + ", " + b.author + ", "
+ b.publisher + ", " + b.quantity);
}
}
}

预订

public class Book implements Comparable<Book> {

int id;
String name, author, publisher;
int quantity;

public Book(int id, String name, String author, String publisher, int quantity) {

this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}


public int compareTo(Book b) {

if(id > b.id) {

return 1;
} else if (id < b.id) {

return -1;
} else {

return 0;
}
}
}

控制台中的结果:

101, Data Communications & Networking, Forouzan, Mc Graw Hill, 4
121, Let us C, Yashwant Kanetkar, BPB, 8
191, Linux, Hadgy, Haman, 7
233, Operating System, Galvin, Wiley, 6
287, Python Programming, Tarzan, GEN, 5
319, Learn Java, Yanis Orhan, HEX, 3

为什么当我打印 ArrayList 时 for-each 循环工作得很好,而当我尝试打印 PriorityQueue 时却不起作用?

最佳答案

Javadoc says :

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

因此,您不能仅使用 for(Book book:queue) 循环。

您可以按照 Javadoc 中的建议复制到临时数组中,或者(如果您不介意在此过程中破坏队列)循环 poll()

我同意这是非常违反直觉的。他们可能根本不应该实现Iterable...

关于java - 为什么 PriorityQueue 需要 Comparable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50369803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com