FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/onjava/CountMap.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
/
onjava
/
CountMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (89 loc) · 2.41 KB
Breadcrumbs
OnJava8-Examples
/
onjava
/
CountMap.java
Copy path
File metadata and controls
91 lines (89 loc) · 2.41 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
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
88
89
90
91
// onjava/CountMap.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.
// Unlimited-length Map containing sample data
// {java onjava.CountMap}
package
onjava
;
import
java
.
util
.*;
import
java
.
util
.
stream
.*;
public
class
CountMap
extends
AbstractMap
<
Integer
,
String
> {
private
int
size
;
private
static
char
[]
chars
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.
toCharArray
();
private
static
String
value
(
int
key
) {
return
chars
[
key
%
chars
.
length
] +
Integer
.
toString
(
key
/
chars
.
length
);
}
public
CountMap
(
int
size
) {
this
.
size
=
size
<
0
?
0
:
size
;
}
@
Override
public
String
get
(
Object
key
) {
return
value
((
Integer
)
key
);
}
private
static
class
Entry
implements
Map
.
Entry
<
Integer
,
String
> {
int
index
;
Entry
(
int
index
) {
this
.
index
=
index
; }
@
Override
public
boolean
equals
(
Object
o
) {
return
o
instanceof
Entry
&&
Objects
.
equals
(
index
, ((
Entry
)
o
).
index
);
}
@
Override
public
Integer
getKey
() {
return
index
; }
@
Override
public
String
getValue
() {
return
value
(
index
);
}
@
Override
public
String
setValue
(
String
value
) {
throw
new
UnsupportedOperationException
();
}
@
Override
public
int
hashCode
() {
return
Objects
.
hashCode
(
index
);
}
}
@
Override
public
Set
<
Map
.
Entry
<
Integer
,
String
>>
entrySet
() {
// LinkedHashSet retains initialization order:
return
IntStream
.
range
(
0
,
size
)
.
mapToObj
(
Entry
::
new
)
.
collect
(
Collectors
.
toCollection
(
LinkedHashSet
::
new
));
}
public
static
void
main
(
String
[]
args
) {
final
int
size
=
6
;
CountMap
cm
=
new
CountMap
(
60
);
System
.
out
.
println
(
cm
);
System
.
out
.
println
(
cm
.
get
(
500
));
cm
.
values
().
stream
()
.
limit
(
size
)
.
forEach
(
System
.
out
::
println
);
System
.
out
.
println
();
new
Random
(
47
).
ints
(
size
,
0
,
1000
)
.
mapToObj
(
cm
::
get
)
.
forEach
(
System
.
out
::
println
);
}
}
/* Output:
{0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0,
9=J0, 10=K0, 11=L0, 12=M0, 13=N0, 14=O0, 15=P0, 16=Q0,
17=R0, 18=S0, 19=T0, 20=U0, 21=V0, 22=W0, 23=X0, 24=Y0,
25=Z0, 26=A1, 27=B1, 28=C1, 29=D1, 30=E1, 31=F1, 32=G1,
33=H1, 34=I1, 35=J1, 36=K1, 37=L1, 38=M1, 39=N1, 40=O1,
41=P1, 42=Q1, 43=R1, 44=S1, 45=T1, 46=U1, 47=V1, 48=W1,
49=X1, 50=Y1, 51=Z1, 52=A2, 53=B2, 54=C2, 55=D2, 56=E2,
57=F2, 58=G2, 59=H2}
G19
A0
B0
C0
D0
E0
F0
Y9
J21
R26
D33
Z36
N16
*/
Back
|
FazBrowse Home
|
New Git URL