-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQ.java
35 lines (26 loc) · 928 Bytes
/
PriorityQ.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Priority Queue
In priority queue, the elements are ordered according to the key values.
The element with the lowest key value may be the head of the queue
or it may be the last element in the queue depending on the programming language.
*/
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQ{
public static void main(String[] args){
PriorityQueue<String> queue = new PriorityQueue<String>(10, new Comparator<String>(){
public int compare(String car1, String car2){
int price1 = Integer.parseInt(car1.split(":")[1].trim());
int price2 = Integer.parseInt(car2.split(":")[1].trim());
return price2 - price1;
}
});
queue.add("Maserati GranTurismo: 523000");
queue.add("MINI Cooper: 150800");
queue.add("Lexus: 220000");
queue.add("McLaren: 998000");
queue.add("Ferrari: 1160000");
while(!queue.isEmpty()){
System.out.println(queue.poll());
}
}
}