FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/streams/MapCollector.java at master · xinan-w/OnJava8-Examples · GitHub
xinan-w
/
OnJava8-Examples
Public
forked from
BruceEckel/OnJava8-Examples
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
streams
/
MapCollector.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (44 loc) · 1.18 KB
Breadcrumbs
OnJava8-Examples
/
streams
/
MapCollector.java
Copy path
File metadata and controls
47 lines (44 loc) · 1.18 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
// streams/MapCollector.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
.*;
import
java
.
util
.
stream
.*;
class
Pair
{
public
final
Character
c
;
public
final
Integer
i
;
Pair
(
Character
c
,
Integer
i
) {
this
.
c
=
c
;
this
.
i
=
i
;
}
public
Character
getC
() {
return
c
; }
public
Integer
getI
() {
return
i
; }
@
Override
public
String
toString
() {
return
"Pair("
+
c
+
", "
+
i
+
")"
;
}
}
class
RandomPair
{
Random
rand
=
new
Random
(
47
);
// An infinite iterator of random capital letters:
Iterator
<
Character
>
capChars
=
rand
.
ints
(
65
,
91
)
.
mapToObj
(
i
-> (
char
)
i
)
.
iterator
();
public
Stream
<
Pair
>
stream
() {
return
rand
.
ints
(
100
,
1000
).
distinct
()
.
mapToObj
(
i
->
new
Pair
(
capChars
.
next
(),
i
));
}
}
public
class
MapCollector
{
public
static
void
main
(
String
[]
args
) {
Map
<
Integer
,
Character
>
map
=
new
RandomPair
().
stream
()
.
limit
(
8
)
.
collect
(
Collectors
.
toMap
(
Pair
::
getI
,
Pair
::
getC
));
System
.
out
.
println
(
map
);
}
}
/* Output:
{688=W, 309=C, 293=B, 761=N, 858=N, 668=G, 622=F,
751=N}
*/
Back
|
FazBrowse Home
|
New Git URL