FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/DesignHashMap.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
DesignHashMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
81 lines (65 loc) · 2.29 KB
Breadcrumbs
leetcode-algorithms
/
src
/
DesignHashMap.java
Copy path
File metadata and controls
81 lines (65 loc) · 2.29 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
import
java
.
util
.
LinkedList
;
import
java
.
util
.
Objects
;
import
java
.
util
.
Queue
;
public
class
DesignHashMap
{
class
MyHashMap
{
private
final
int
initialSize
=
100
;
private
final
Bucket
[]
buckets
=
new
Bucket
[
initialSize
];
public
MyHashMap
() {
for
(
int
index
=
0
;
index
<
buckets
.
length
;
index
++) {
buckets
[
index
] =
new
Bucket
();
}
}
public
void
put
(
int
key
,
int
value
) {
int
hashCode
=
key
%
buckets
.
length
;
buckets
[
hashCode
].
put
(
key
,
value
);
}
public
int
get
(
int
key
) {
return
buckets
[
key
%
buckets
.
length
].
get
(
key
);
}
public
void
remove
(
int
key
) {
buckets
[
key
%
buckets
.
length
].
remove
(
key
);
}
private
class
Bucket
{
Queue
<
Pair
>
list
=
new
LinkedList
<>();
public
boolean
contains
(
int
element
) {
return
list
.
stream
().
anyMatch
(
pair
->
pair
.
key
==
element
);
}
public
void
put
(
int
key
,
int
value
) {
if
(
contains
(
key
)) {
Pair
match
=
list
.
stream
().
filter
(
pair
->
pair
.
key
==
key
).
findFirst
().
get
();
match
.
value
=
value
;
}
else
{
list
.
add
(
new
Pair
(
key
,
value
));
}
}
public
int
get
(
int
key
) {
return
contains
(
key
) ?
list
.
stream
().
filter
(
pair
->
pair
.
key
==
key
).
findFirst
().
get
().
value
: -
1
;
}
public
void
remove
(
int
key
) {
if
(
contains
(
key
)) {
list
.
remove
(
new
Pair
(
key
,
get
(
key
)));
}
}
}
private
class
Pair
{
private
final
int
key
;
private
int
value
;
Pair
(
int
key
,
int
value
) {
this
.
key
=
key
;
this
.
value
=
value
;
}
@
Override
public
boolean
equals
(
Object
o
) {
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
() !=
o
.
getClass
())
return
false
;
Pair
pair
= (
Pair
)
o
;
return
key
==
pair
.
key
&&
value
==
pair
.
value
;
}
@
Override
public
int
hashCode
() {
return
Objects
.
hash
(
key
,
value
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL