FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/equalshashcode/SimpleHashMap.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
/
SimpleHashMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (86 loc) · 2.44 KB
Breadcrumbs
OnJava8-Examples
/
equalshashcode
/
SimpleHashMap.java
Copy path
File metadata and controls
87 lines (86 loc) · 2.44 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
// equalshashcode/SimpleHashMap.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 demonstration hashed Map
import
java
.
util
.*;
import
onjava
.*;
public
class
SimpleHashMap
<
K
,
V
>
extends
AbstractMap
<
K
,
V
> {
// Choose a prime number for the hash table
// size, to achieve a uniform distribution:
static
final
int
SIZE
=
997
;
// You can't have a physical array of generics,
// but you can upcast to one:
@
SuppressWarnings
(
"unchecked"
)
LinkedList
<
MapEntry
<
K
,
V
>>[]
buckets
=
new
LinkedList
[
SIZE
];
@
Override
public
V
put
(
K
key
,
V
value
) {
V
oldValue
=
null
;
int
index
=
Math
.
abs
(
key
.
hashCode
()) %
SIZE
;
if
(
buckets
[
index
] ==
null
)
buckets
[
index
] =
new
LinkedList
<>();
LinkedList
<
MapEntry
<
K
,
V
>>
bucket
=
buckets
[
index
];
MapEntry
<
K
,
V
>
pair
=
new
MapEntry
<>(
key
,
value
);
boolean
found
=
false
;
ListIterator
<
MapEntry
<
K
,
V
>>
it
=
bucket
.
listIterator
();
while
(
it
.
hasNext
()) {
MapEntry
<
K
,
V
>
iPair
=
it
.
next
();
if
(
iPair
.
getKey
().
equals
(
key
)) {
oldValue
=
iPair
.
getValue
();
it
.
set
(
pair
);
// Replace old with new
found
=
true
;
break
;
}
}
if
(!
found
)
buckets
[
index
].
add
(
pair
);
return
oldValue
;
}
@
Override
public
V
get
(
Object
key
) {
int
index
=
Math
.
abs
(
key
.
hashCode
()) %
SIZE
;
if
(
buckets
[
index
] ==
null
)
return
null
;
for
(
MapEntry
<
K
,
V
>
iPair
:
buckets
[
index
])
if
(
iPair
.
getKey
().
equals
(
key
))
return
iPair
.
getValue
();
return
null
;
}
@
Override
public
Set
<
Map
.
Entry
<
K
,
V
>>
entrySet
() {
Set
<
Map
.
Entry
<
K
,
V
>>
set
=
new
HashSet
<>();
for
(
LinkedList
<
MapEntry
<
K
,
V
>>
bucket
:
buckets
) {
if
(
bucket
==
null
)
continue
;
for
(
MapEntry
<
K
,
V
>
mpair
:
bucket
)
set
.
add
(
mpair
);
}
return
set
;
}
public
static
void
main
(
String
[]
args
) {
SimpleHashMap
<
String
,
String
>
m
=
new
SimpleHashMap
<>();
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