通常,队列遵循先进先出(fifo)的原则,但是priorityqueue按照优先级的方式访问元素时,遵循一种基于优先级的方法。队列的每个元素具有与之关联的优先级。元素根据自然排序进行优先排序order. however, we can provide custom orders using a comparator. the elements ofpriorityqueue are not actually sorted, they are only retrieved in sorted order. this featureallows us to modify an element of priorityqueue easily.
java program to modify an element of a prorityqueue在开始程序之前,让我们先了解一下priorityqueue的几个内置方法 -
add() − it is used to add a single element to the queue
offer() − 它还将给定的元素插入到队列中。
peek() − 用于检索队列的第一个元素。
remove() − it is used to remove the specified element from the queue.
approach 1定义一个priorityqueue集合的对象,并使用'add()'方法存储一些元素
method.now, using ‘peek()’ method show the first element of queue and then remove this使用 'remove()' 方法从队列中移除元素
进一步移动,使用内置方法在相同位置插入一个新元素
‘offer()’.again show the modified first element.
example 的中文翻译为:示例in the following example, we will modify an element of priorityqueue. the elements are
没有比较器的优先级,这意味着它们将按升序访问。
import java.util.*;public class modify { public static void main(string[] args) { priorityqueue<integer> queuepq = new priorityqueue<>(); // inserting elements queuepq.add(7); queuepq.add(9); queuepq.add(2); queuepq.add(4); queuepq.add(3); system.out.println(original queue: + queuepq); int head1 = queuepq.peek(); // accessing first element system.out.println(the first element in queue: + head1); queuepq.remove(2); // removing first element queuepq.offer(1); // adding new element at first position int head2 = queuepq.peek(); // accessing first element system.out.println(the updated first element in queue: + head2); queuepq.offer(2); // adding new element at first position system.out.println(newly updated queue: + queuepq); }}
输出original queue: [2, 3, 7, 9, 4]the first element in queue: 2the updated first element in queue: 1newly updated queue: [1, 3, 2, 9, 4, 7]
方法二使用一个名为的内置方法定义一个priorityqueue集合的对象‘collections.reverseorder()’用于将元素按降序存储。
现在,按照前面示例中描述的相同步骤进行。
example 的中文翻译为:示例 in the following example, we will modify an element of priorityqueue. the elements are使用比较器进行优先排序,以便按降序提供访问。
import java.util.*;public class modify { public static void main(string[] args) { priorityqueue<integer> queuepq = new priorityqueue<>(collections.reverseorder()); // inserting elements queuepq.add(7); queuepq.add(9); queuepq.add(2); queuepq.add(1); queuepq.add(3); system.out.println(original queue: + queuepq); int head1 = queuepq.peek(); // accessing first element system.out.println(the first element in queue: + head1); queuepq.remove(9); // removing first element queuepq.offer(8); // adding new element at first position int head2 = queuepq.peek(); // accessing first element system.out.println(the updated first element in queue: + head2); queuepq.offer(9); // adding new element at first position system.out.println(newly updated queue: + queuepq); }}
输出original queue: [9, 7, 2, 1, 3]the first element in queue: 9the updated first element in queue: 8newly updated queue: [9, 7, 8, 1, 3, 2]
结论我们通过定义java collection framework的priorityqueue类来开始本文实现queue接口的内容。在下一节中,我们将讨论一些内置的内容在给定的priorityqueue中修改元素的java程序中使用的方法
以上就是我如何在java中修改priorityqueue的元素?的详细内容。