Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 508afc8

Browse files
committed
Added Priority Queue using comparator
1 parent 8dec66f commit 508afc8

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package stacks_queues;
2+
3+
import java.util.Comparator;
4+
5+
public class IntegerValComparator implements Comparator<Integer> {
6+
7+
@Override
8+
public int compare(Integer o1, Integer o2) {
9+
// TODO Auto-generated method stub
10+
return o2-o1;
11+
// if(o1<o2){
12+
// return 1;
13+
// } else {
14+
// return -1;
15+
// }
16+
}
17+
18+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package stacks_queues;
2+
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
9+
public class PriQueueExample {
10+
11+
public static void main(String[] args) {
12+
13+
Comparator<Integer> comp1 = new IntegerValComparator();
14+
15+
PriorityQueue<Integer> test = new PriorityQueue<>(comp1);
16+
17+
HashMap<Integer,Integer> testhmap = new HashMap<>();
18+
19+
// for(int i = 1; i < 11; i++){
20+
// test.add(100-i);
21+
// testhmap.put(i, i*5);
22+
//
23+
// }
24+
25+
test.add(20);
26+
test.add(40);
27+
test.add(66);
28+
test.add(74);
29+
test.add(98);
30+
test.add(12);
31+
test.add(25);
32+
test.add(81);
33+
test.add(66);
34+
test.add(72);
35+
36+
37+
System.out.println(test.remove());
38+
System.out.println(test.remove());
39+
System.out.println(test.remove());
40+
41+
42+
// Iterator it = test.iterator();
43+
// while(it.hasNext()){
44+
// System.out.println(it.next());
45+
// }
46+
47+
// Iterator hit = testhmap.entrySet().iterator();
48+
// while(hit.hasNext()){
49+
// System.out.println(hit.next());
50+
// }
51+
//test.add(testhmap);
52+
53+
// for (Map.Entry m : testhmap.entrySet()){
54+
// System.out.println("5 x "+m.getKey()+" = "+m.getValue());
55+
// }
56+
57+
//System.out.println(test.peek());
58+
59+
}
60+
61+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /