FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/equalshashcode/SlowMap.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
/
equalshashcode
/
SlowMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (61 loc) · 1.65 KB
Breadcrumbs
OnJava8-Examples
/
equalshashcode
/
SlowMap.java
Copy path
File metadata and controls
62 lines (61 loc) · 1.65 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
// equalshashcode/SlowMap.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.
// A Map implemented with ArrayLists
import
java
.
util
.*;
import
onjava
.*;
public
class
SlowMap
<
K
,
V
>
extends
AbstractMap
<
K
,
V
> {
private
List
<
K
>
keys
=
new
ArrayList
<>();
private
List
<
V
>
values
=
new
ArrayList
<>();
@
Override
public
V
put
(
K
key
,
V
value
) {
V
oldValue
=
get
(
key
);
// The old value or null
if
(!
keys
.
contains
(
key
)) {
keys
.
add
(
key
);
values
.
add
(
value
);
}
else
values
.
set
(
keys
.
indexOf
(
key
),
value
);
return
oldValue
;
}
@
Override
public
V
get
(
Object
key
) {
// key: type Object, not K
if
(!
keys
.
contains
(
key
))
return
null
;
return
values
.
get
(
keys
.
indexOf
(
key
));
}
@
Override
public
Set
<
Map
.
Entry
<
K
,
V
>>
entrySet
() {
Set
<
Map
.
Entry
<
K
,
V
>>
set
=
new
HashSet
<>();
Iterator
<
K
>
ki
=
keys
.
iterator
();
Iterator
<
V
>
vi
=
values
.
iterator
();
while
(
ki
.
hasNext
())
set
.
add
(
new
MapEntry
<>(
ki
.
next
(),
vi
.
next
()));
return
set
;
}
public
static
void
main
(
String
[]
args
) {
SlowMap
<
String
,
String
>
m
=
new
SlowMap
<>();
m
.
putAll
(
Countries
.
capitals
(
8
));
m
.
forEach
((
k
,
v
) ->
System
.
out
.
println
(
k
+
"="
+
v
));
System
.
out
.
println
(
m
.
get
(
"BENIN"
));
m
.
entrySet
().
forEach
(
System
.
out
::
println
);
}
}
/* Output:
CAMEROON=Yaounde
ANGOLA=Luanda
BURKINA FASO=Ouagadougou
BURUNDI=Bujumbura
ALGERIA=Algiers
BENIN=Porto-Novo
CAPE VERDE=Praia
BOTSWANA=Gaberone
Porto-Novo
CAMEROON=Yaounde
ANGOLA=Luanda
BURKINA FASO=Ouagadougou
BURUNDI=Bujumbura
ALGERIA=Algiers
BENIN=Porto-Novo
CAPE VERDE=Praia
BOTSWANA=Gaberone
*/
Back
|
FazBrowse Home
|
New Git URL