Knowee
Questions
Features
Study Tools

Create a PriorityQueue of strings.Add at least five strings to the PriorityQueue.Print the elements of the PriorityQueue.Remove and print the elements from the PriorityQueue one by one. Observe how the elements are removed in the order of their natural ordering (min-heap).Create a "custom comparator" to change the order of the PriorityQueue based on the length of the string, longest string has highest priority.Add more strings to the PriorityQueue with the custom comparator.Print the elements of the changed PriorityQueue.

Question

Create a PriorityQueue of strings.Add at least five strings to the PriorityQueue.Print the elements of the PriorityQueue.Remove and print the elements from the PriorityQueue one by one. Observe how the elements are removed in the order of their natural ordering (min-heap).Create a "custom comparator" to change the order of the PriorityQueue based on the length of the string, longest string has highest priority.Add more strings to the PriorityQueue with the custom comparator.Print the elements of the changed PriorityQueue.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to create a PriorityQueue of strings, add strings, print elements, remove elements, create a custom comparator, and print the elements of the changed PriorityQueue in Java.

Step 1: Create a PriorityQueue of strings.

PriorityQueue<String> pq = new PriorityQueue<>();

Step 2: Add at least five strings to the PriorityQueue.

pq.add("Apple");
pq.add("Banana");
pq.add("Cherry");
pq.add("Date");
pq.add("Elderberry");

Step 3: Print the elements of the PriorityQueue.

System.out.println(pq);

Step 4: Remove and print the elements from the PriorityQueue one by one.

while(!pq.isEmpty()) {
    System.out.println(pq.poll());
}

Step 5: Create a "custom comparator" to change the order of the PriorityQueue based on the length of the string, longest string has highest priority.

Comparator<String> stringLengthComparator = new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return s2.length() - s1.length();
    }
};

Step 6: Create a new PriorityQueue with the custom comparator.

PriorityQueue<String> pq2 = new PriorityQueue<>(stringLengthComparator);

Step 7: Add more strings to the PriorityQueue with the custom comparator.

pq2.add("Fig");
pq2.add("Grapefruit");
pq2.add("Honeydew");
pq2.add("Ice-cream");
pq2.add("Jackfruit");

Step 8: Print the elements of the changed PriorityQueue.

while(!pq2.isEmpty()) {
    System.out.println(pq2.poll());
}

This will print the elements in the order of their length, from longest to shortest.

This problem has been solved

Similar Questions

Create an ArrayList of strings.Add five strings to the ArrayList.Print the elements of the ArrayList.Remove an element from the ArrayList.Print the updated ArrayList.Search for a specific element in the ArrayList and display the result.Sort the ArrayList in descending order using the Collections.sort() method.Print the sorted ArrayList.

A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is given below: 10, 8, 5, 3, 2 Two new elements ”1‘ and ”7‘ are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the elements is:10, 8, 7, 5, 3, 2, 110, 8, 7, 5, 3, 1, 510, 8, 7, 5, 2, 1, 510, 8, 7, 3, 2, 3, 5

A priority queue is implemented as a max-heap initially, it has five elements. The level-order traversal of theheap is as follows: 20, 18, 15, 13, 12. Suppose two new elements ‘10’ and ‘17’ are inserted in the heap inthat order. Find the level-order traversal of the heap after the insertion of the element.

Perform Insert, delMin and getMin operations on a Min Heap. Implement Min Heaps and avoid using inbuilt library.Input FormatFirst line of input contains T - number of operations. Its followed by T lines, each line contains either "insert x" or "delMin" or "getMin".Constraints1 <= T <= 106-109 <= x <= 109Output FormatFor each "getMin" operation, print the minimum element, separated by newline. If the heap is empty, print "Empty".Sample Input 010insert 5getMindelMingetMininsert -15insert 10insert -30getMindelMingetMinSample Output 05Empty-30-15Explanation 0Self Explanatory

Consider the following program.• Fill in the blank at LINE-1 with the appropriate header of max str( ).• Fill in the blank at LINE-2 with the appropriate statements such that the string array can be sorted in descending order.The program must satisfy the sample input and output.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.