FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/collections/PriorityQueueDemo.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
collections
/
PriorityQueueDemo.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (49 loc) · 1.74 KB
Breadcrumbs
OnJava8-Examples
/
collections
/
PriorityQueueDemo.java
Copy path
File metadata and controls
53 lines (49 loc) · 1.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// collections/PriorityQueueDemo.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import
java
.
util
.*;
public
class
PriorityQueueDemo
{
public
static
void
main
(
String
[]
args
) {
PriorityQueue
<
Integer
>
priorityQueue
=
new
PriorityQueue
<>();
Random
rand
=
new
Random
(
47
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
priorityQueue
.
offer
(
rand
.
nextInt
(
i
+
10
));
QueueDemo
.
printQ
(
priorityQueue
);
List
<
Integer
>
ints
=
Arrays
.
asList
(
25
,
22
,
20
,
18
,
14
,
9
,
3
,
1
,
1
,
2
,
3
,
9
,
14
,
18
,
21
,
23
,
25
);
priorityQueue
=
new
PriorityQueue
<>(
ints
);
QueueDemo
.
printQ
(
priorityQueue
);
priorityQueue
=
new
PriorityQueue
<>(
ints
.
size
(),
Collections
.
reverseOrder
());
priorityQueue
.
addAll
(
ints
);
QueueDemo
.
printQ
(
priorityQueue
);
String
fact
=
"EDUCATION SHOULD ESCHEW OBFUSCATION"
;
List
<
String
>
strings
=
Arrays
.
asList
(
fact
.
split
(
""
));
PriorityQueue
<
String
>
stringPQ
=
new
PriorityQueue
<>(
strings
);
QueueDemo
.
printQ
(
stringPQ
);
stringPQ
=
new
PriorityQueue
<>(
strings
.
size
(),
Collections
.
reverseOrder
());
stringPQ
.
addAll
(
strings
);
QueueDemo
.
printQ
(
stringPQ
);
Set
<
Character
>
charSet
=
new
HashSet
<>();
for
(
char
c
:
fact
.
toCharArray
())
charSet
.
add
(
c
);
// Autoboxing
PriorityQueue
<
Character
>
characterPQ
=
new
PriorityQueue
<>(
charSet
);
QueueDemo
.
printQ
(
characterPQ
);
}
}
/* Output:
0 1 1 1 1 1 3 5 8 14
1 1 2 3 3 9 9 14 14 18 18 20 21 22 23 25 25
25 25 23 22 21 20 18 18 14 14 9 9 3 3 2 1 1
A A B C C C D D E E E F H H I I L N N O O O O S S
S T T U U U W
W U U U T T S S S O O O O N N L I I H H F E E E D D C C
C B A A
A B C D E F H I L N O S T U W
*/
Back
|
FazBrowse Home
|
New Git URL