-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.java
More file actions
60 lines (48 loc) · 1.66 KB
/
Basics.java
File metadata and controls
60 lines (48 loc) · 1.66 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
package priorityQueue;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Basics {
public static void main(String[] args) {
// DEFAULT: Min Heap.
PriorityQueue<Integer> pq = new PriorityQueue<>();
PriorityQueue<Integer> temp = new PriorityQueue<>(20);
// Max Heap.
PriorityQueue<Integer> max = new PriorityQueue<>(Comparator.reverseOrder());
pq.add(10);
pq.offer(5);
pq.add(20);
System.out.println(pq.remove());
System.out.println(pq.peek());
PriorityQueue<Student> studentPQ = new PriorityQueue<>((a, b) -> a.rollNo - b.rollNo);
Student student1 = new Student("Deep", 29, 93);
Student student2 = new Student("Parth", 30, 85);
studentPQ.add(student1);
studentPQ.add(student2);
System.out.println(studentPQ);
}
private static class Student {
String name;
Integer rollNo;
Integer marks;
Student(String name, Integer rollNo, Integer marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
@Override
public String toString() {
return "Student{ " + "name='" + name + '\'' + ", rollNo=" + rollNo + ", marks=" + marks + '}';
}
}
private static class StudentComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
if (s1.marks > s2.marks) {
return 1;
} else if (s1.marks < s2.marks) {
return -1;
} else {
return s1.rollNo.compareTo(s2.rollNo);
}
}
}
}