-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjava-priority-queue-examples
More file actions
87 lines (69 loc) · 2.94 KB
/
java-priority-queue-examples
File metadata and controls
87 lines (69 loc) · 2.94 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.PriorityQueue;
public class CreatePriorityQueueExample {
public static void main(String[] args) {
// Create a Priority Queue
PriorityQueue<Integer> numbers = new PriorityQueue<>();
// Add items to a Priority Queue (ENQUEUE)
numbers.add(750);
numbers.add(500);
numbers.add(900);
numbers.add(100);
// Remove items from the Priority Queue (DEQUEUE)
while (!numbers.isEmpty()) {
System.out.println(numbers.remove());
}
}
}
**************************************************************************************************************************************************************************
import java.util.PriorityQueue;
public class CreatePriorityQueueStringExample {
public static void main(String[] args) {
// Create a Priority Queue
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>();
// Add items to a Priority Queue (ENQUEUE)
namePriorityQueue.add("Lisa");
namePriorityQueue.add("Robert");
namePriorityQueue.add("John");
namePriorityQueue.add("Chris");
namePriorityQueue.add("Angelina");
namePriorityQueue.add("Joe");
// Remove items from the Priority Queue (DEQUEUE)
while (!namePriorityQueue.isEmpty()) {
System.out.println(namePriorityQueue.remove());
}
}
}
************************************************************************************************************************************************************************
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQueueCustomComparatorExample {
public static void main(String[] args) {
Comparator<String> stringLengthComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
};
/*
The above Comparator can also be created using lambda expression like this =>
Comparator<String> stringLengthComparator = (s1, s2) -> {
return s1.length() - s2.length();
};
The above statement can be shortened even further like this =>
Comparator<String> stringLengthComparator = Comparator.comparingInt(String::length);
*/
// Create a Priority Queue with a custom Comparator
PriorityQueue<String> namePriorityQueue = new PriorityQueue<>(stringLengthComparator);
// Add items to a Priority Queue (ENQUEUE)
namePriorityQueue.add("Lisa");
namePriorityQueue.add("Robert");
namePriorityQueue.add("John");
namePriorityQueue.add("Chris");
namePriorityQueue.add("Angelina");
namePriorityQueue.add("Joe");
// Remove items from the Priority Queue (DEQUEUE)
while (!namePriorityQueue.isEmpty()) {
System.out.println(namePriorityQueue.remove());
}
}
}