-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyHashMap.java
More file actions
145 lines (126 loc) · 3.73 KB
/
MyHashMap.java
File metadata and controls
145 lines (126 loc) · 3.73 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.yurii.salimov.lesson10.task06;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public final class MyHashMap<K, V> implements MyMap<K, V> {
public static class Entry<K, V> {
private K key;
private V value;
public Entry(final K key, final V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return this.key;
}
public V getValue() {
return this.value;
}
public void setValue(final V value) {
this.value = value;
}
}
private static final int DEFAULT_CAPACITY = 16;
private List[] arr;
public MyHashMap(final int capacity) {
this.arr = new LinkedList[capacity];
}
public MyHashMap() {
this.arr = new LinkedList[DEFAULT_CAPACITY];
}
@Override
public boolean containsValue(final V value) {
List<Entry<K, V>> list;
for (List ar : this.arr) {
if (ar != null) {
list = (LinkedList<Entry<K, V>>) ar;
for (Entry<K, V> entry : list) {
if (entry.getValue().equals(value)) {
return true;
}
}
}
}
return false;
}
@Override
public Set<K> keySet() {
final Set<K> set = new HashSet<>();
LinkedList<Entry<K, V>> list;
for (List ar : this.arr) {
if (ar != null) {
list = (LinkedList<Entry<K, V>>) ar;
set.addAll(list.stream().map(Entry::getKey).collect(Collectors.toList()));
}
}
return set;
}
public Set<Entry<K, V>> entrySet() {
final Set<Entry<K, V>> set = new HashSet<>();
LinkedList<Entry<K, V>> list;
for (List ar : this.arr) {
if (ar != null) {
list = (LinkedList<Entry<K, V>>) ar;
set.addAll(list);
}
}
return set;
}
@Override
public void put(final K key, final V value) {
final Entry<K, V> entry = new Entry<>(key, value);
final int hashCode = key.hashCode();
final int index = Math.abs(hashCode % this.arr.length);
if (this.arr[index] == null) {
arr[index] = new LinkedList<>();
}
final List<Entry<K, V>> list = (LinkedList<Entry<K, V>>) arr[index];
for (Entry<K, V> ent : list) {
if (ent.getKey().equals(entry.getKey())) {
ent.setValue(entry.getValue());
return;
}
}
list.add(entry);
}
@Override
public V get(final K key) {
final int hashCode = key.hashCode();
final int index = Math.abs(hashCode % this.arr.length);
if (this.arr[index] == null) {
return null;
}
final LinkedList<Entry<K, V>> list = (LinkedList<Entry<K, V>>) this.arr[index];
for (Entry<K, V> entry : list) {
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}
@Override
public boolean containsKey(final K key) {
return get(key) != null;
}
@Override
public int size() {
List<Entry<K, V>> list;
int size = 0;
for (Object ar : this.arr) {
list = (LinkedList<Entry<K, V>>) ar;
if (list != null) {
size += list.size();
}
}
return size;
}
public void setCapacity(final int capacity) {
this.arr = new LinkedList[capacity];
}
}