﻿ Java: Change priorityQueue to maximum priorityqueue

# Java Collection, PriorityQueue Exercises: Change priorityQueue to maximum priorityqueue

## Java Collection, PriorityQueue Exercises: Exercise-12 with Solution

Write a Java program to change priorityQueue to maximum priorityqueue.

Sample Solution:-

Java Code:

``````import java.util.*;
public class Example12 {
public static void main(String[] args) {
PriorityQueue<Integer> pq1 = new PriorityQueue<>(10, Collections.reverseOrder());

// Add numbers in the Queue
System.out.println("\nOriginal Priority Queue: "+pq1);

System.out.print("\nMaximum Priority Queue: ");
Integer val = null;
while( (val = pq1.poll()) != null) {
System.out.print(val+"  ");
}
System.out.print("\n");
}
}
``````

Sample Output:

```Original Priority Queue: [89, 82, 70, 25, 16, 22, 36, 10, 14]

Maximum Priority Queue: 89  82  70  36  25  22  16  14  10```

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Java: Tips of the Day

getEnumMap

Converts to enum to Map where key is the name and value is Enum itself.

```public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.collect(Collectors.toMap(Enum::name, Function.identity()));
}
```

Ref: https://bit.ly/3xXcFZt