- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习队列集合,但我不明白为什么如果使用 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 时却不起作用?
最佳答案
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/
采用另一个优先级队列的 java API PriorityQueue 构造函数是否会破坏参数?如果是这样,它的clone()方法足以创建浅拷贝吗? 最佳答案 不,它不是破坏性的。几乎所有集合类都有复制
我正在尝试复制一个 PriorityQueue 对象。 我的目标是在不修改我原来的 PriorityQueue 的情况下更改我的 Copy 的某些对象 为了这样做,我复制了我的 PriorityQue
我正在解决leetcode的Merge K Sorted Lists problem . 使用 Python2 的 Queue 模块中的 PriorityQueue 的相同算法会为 Python3 的
当 PriorityQueue.size() > 0 在 Android 上时,当 PriorityQueue.peek() 返回 null 时,我遇到了问题。 我认为这可能是设备问题。有人有什么想法
我有一个asyncio.PriorityQueue,用作网络爬虫的URL队列,当我调用url_queue.get时,得分最低的URL首先从队列中删除()。当队列达到 maxsize 项时,默认行为是阻
完全二叉树 一棵深度为k的有n个结点的 二叉树 ,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与 满二叉树 中编号为i的结点在二叉树中的位置相同,则这棵
我很难理解 priorityqueue 如何使用 compareTo 方法对其内容进行排序。 我在上一门名为 Node.js 的类(class)。它有 4 个字段。 private char char
我目前有一种方法使用 scala.collection.mutable.PriorityQueue 按特定顺序组合元素。例如代码看起来有点像这样: def process[A : Ordering]
没有提供自定义比较器,优先级队列按升序插入元素,但是,在删除特定元素后,顺序会发生变化。 PriorityQueue pq = new PriorityQueue<>(); pq.add(10); p
这个问题已经有答案了: The built-in iterator for java's PriorityQueue does not traverse the data structure in a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我指的是此博客上列出的代码:https://strstr.io/Leetcode1054-Distant-Barcodes/ 我在这里复制这段代码 class Solution { publi
这个问题已经有答案了: Why does PriorityQueue.toString return the wrong element order? [duplicate] (4 个回答) 已关闭
我有一个比较器类 NComparator,它比较 2 个 Node 对象并返回 1、-1 或 0。 我初始化了一个初始容量为 100 的 PriorityQueue 和那个 NComparator。
当项目是整数与字符串时,PriorityQueue 的不同行为让我非常困惑。但在解决这个问题之前,我想了解以下行为(使用项目作为整数)。 假设我有一个包含以下数据的优先级队列(对于每个元素,第一个值是
我有使用 PriorityQueue 的程序。 poll() 没有给出队列中的所有值。 class Coffee { public static void main(String[] args
所以我正在尝试构建我的第一个 prim 算法,为此我根据其权重按优先级对边缘进行排序。 所以我认为如果我使用优先级队列会很有帮助,为此我需要让我的边缘实现 Comparable<> 接口(interf
编译器(Java 8)提示以下代码没有合适的构造函数: PriorityQueue heap = new PriorityQueue((ListNode n1, ListNode n2) -> n1.
我有一个优先级队列,我在其中添加一个节点对象,其中节点应按它们包含的值排序。由于某种原因,优先级队列不会在添加时对节点进行排序。如果有人能发现其中的问题或有任何指导,我很感激。这是一个简短的示例: P
这是我在这里发表的第一篇文章,因此请随时为我指出关于在这里提出问题的正确方向。 我的问题出在 java.util.PriorityQueue 上。 我有一个初始化的队列; myComparab
我是一名优秀的程序员,十分优秀!