我如何在Java中修改PriorityQueue的元素?

PHPz
PHPz 转载
2023-08-26 10:13:23 911浏览

我如何在Java中修改PriorityQueue的元素?

通常,队列遵循先进先出(FIFO)的原则,但是PriorityQueue 按照优先级的方式访问元素时,遵循一种基于优先级的方法。队列的每个元素 具有与之关联的优先级。元素根据自然排序进行优先排序 order. However, we can provide custom orders using a comparator. The elements of PriorityQueue are not actually sorted, they are only retrieved in sorted order. This feature allows 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: 2
The updated first element in Queue: 1
Newly 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: 9
The updated first element in Queue: 8
Newly updated Queue: [9, 7, 8, 1, 3, 2]

结论

我们通过定义Java Collection Framework的PriorityQueue类来开始本文 实现Queue接口的内容。在下一节中,我们将讨论一些内置的内容 在给定的PriorityQueue中修改元素的Java程序中使用的方法

以上就是我如何在Java中修改PriorityQueue的元素?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除