Java Collection, PriorityQueue Exercises: Count the number of elements in a priority queue
Java Collection, PriorityQueue Exercises: Exercise-6 with Solution
Write a Java program to count the number of elements in a priority queue.
Sample Solution:-
Java Code:
import java.util.PriorityQueue;
public class Exercise6 {
public static void main(String[] args) {
// create an empty Priority Queue
PriorityQueue<String> pq = new PriorityQueue<String>();
// use add() method to add values in the Priority Queue
pq.add("Red");
pq.add("Green");
pq.add("Black");
pq.add("Pink");
pq.add("orange");
System.out.println("Priority Queue: " + pq);
System.out.println("Size of the Priority Queue: " + pq.size());
}
}
Sample Output:
Priority Queue: [Black, Pink, Green, Red, orange] Size of the Priority Queue: 5
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Remove all the elements from a priority queue.
Next: Compare two priority queues.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework