Java PriorityQueue clear() 方法



描述

Java PriorityQueue clear() 方法会移除此队列中的所有元素。当我们想要重用队列或重置队列时,此方法非常有用。

声明

以下是 java.util.PriorityQueue.clear() 方法的声明

public void clear()

参数

返回值

此方法不返回任何值。

异常

清空整数 PriorityQueue 的示例

以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是整数。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
     
      // create an empty priority queue
      PriorityQueue<Integer> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(20);
      queue.add(30);
      queue.add(10);

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);    

      // finding size of this queue
      int retval = queue.size();
      System.out.println("PriorityQueue consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      queue.clear();
      retval = queue.size();
      System.out.println("Now, queue consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

PriorityQueue = [10, 30, 20]
PriorityQueue consists of 3 elements
Performing clear operation !!
Now, queue consists of 0 elements

清空字符串 PriorityQueue 的示例

以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是字符串。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
     
      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);    

      // finding size of this queue
      int retval = queue.size();
      System.out.println("PriorityQueue consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      queue.clear();
      retval = queue.size();
      System.out.println("Now, queue consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

PriorityQueue = [A, B, C]
PriorityQueue consists of 3 elements
Performing clear operation !!
Now, queue consists of 0 elements

清空对象 PriorityQueue 的示例

以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是 Student 对象。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
     
      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);    

      // finding size of this queue
      int retval = queue.size();
      System.out.println("PriorityQueue consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      queue.clear();
      retval = queue.size();
      System.out.println("Now, queue consists of "+ retval +" elements");
   }
} 

class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
PriorityQueue consists of 3 elements
Performing clear operation !!
Now, queue consists of 0 elements
java_util_priorityqueue.htm
广告