Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa2160f

Browse files
author
jsquared21
committed
Add Ex 27.03
1 parent8c2cbd3 commitfa2160f

File tree

10 files changed

+369
-4
lines changed

10 files changed

+369
-4
lines changed

‎Exercise_27/Exercise_27_01/MyHashMap.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public V get(K key) {
104104
returnnull;
105105
}
106106

107-
@Override/** Return true if this mapcontain no entries */
107+
@Override/** Return true if this mapcontains no entries */
108108
publicbooleanisEmpty() {
109109
returnsize ==0;
110110
}

‎Exercise_27/Exercise_27_01/MyMap.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface MyMap<K, V> {
2323
/** Add an entry (key, value) into the map */
2424
publicVput (Kkey,Vvalue);
2525

26-
/** Remove entry forteh specified key */
26+
/** Remove entry forthe specified key */
2727
publicvoidremove(Kkey);
2828

2929
/** Return the number of mappings in this map */
@@ -39,7 +39,7 @@ public static class Entry<K, V> {
3939

4040
publicEntry(Kkey,Vvalue) {
4141
this.key =key;
42-
this.value =value;
42+
this.value =value;
4343
}
4444

4545
publicKgetKey() {

‎Exercise_27/Exercise_27_02/MyMap.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,4 @@ public String toString() {
5555
return"[" +key +", " +value +"]";
5656
}
5757
}
58-
5958
}
2.4 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*********************************************************************************
2+
* (Implement MyMap using open addressing with double hashing) Create a new *
3+
* concrete class that implements MyMap using open addressing with double *
4+
* hashing. For simplicity, use f(key) = key % size as the hash function, where *
5+
* size is the hash-table size. Initially, the hash-table size is 4. The table *
6+
* size is doubled whenever the load factor exceeds the threshold (0.5). *
7+
*********************************************************************************/
8+
publicclassExercise_27_03 {
9+
publicstaticvoidmain(String[]args) {
10+
// Create a map
11+
MyMap<String,Integer>map =newMyHashMap<>();
12+
map.put("Smith",30);
13+
map.put("Anderson",31);
14+
map.put("Lewis",29);
15+
map.put("Cook",29);
16+
map.put("Tom",21);
17+
map.put("Mark",21);
18+
map.put("Smith",65);
19+
map.put("William",21);
20+
21+
System.out.println("Entries in map: " +map);
22+
23+
System.out.println("The age of Lewis is " +
24+
map.get("Lewis"));
25+
26+
System.out.println("The age of William is " +
27+
map.get("William"));
28+
29+
System.out.println("Is Smith in the map? " +
30+
map.containsKey("Smith"));
31+
32+
System.out.println("Is jay in the map? " +
33+
map.containsKey("Jay"));
34+
35+
System.out.println("Is age 33 in the map? " +
36+
map.containsValue(33));
37+
38+
System.out.println("Is age 31 in the map? " +
39+
map.containsValue(31));
40+
41+
System.out.print("Keys in map: ");
42+
for (Stringkey :map.keySet()) {
43+
System.out.print(key +" ");
44+
}
45+
System.out.println();
46+
47+
System.out.print("Values in map: ");
48+
for (intvalue :map.values()) {
49+
System.out.print(value +" ");
50+
}
51+
System.out.println();
52+
53+
map.remove("Smith");
54+
System.out.println("Entries in map " +map);
55+
56+
map.clear();
57+
System.out.println("Entries in map " +map);
58+
}
59+
}
4.38 KB
Binary file not shown.
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
importjava.util.ArrayList;
2+
3+
publicclassMyHashMap<K,V>implementsMyMap<K,V> {
4+
// Define the default hash-table size. Must be a power of 2
5+
privatestaticintDEFAULT_INITIAL_CAPACITY =4;
6+
7+
// Define the maximum hash-table size. 1 << 30 is same as 2 ^ 30
8+
privatestaticintMAMIMUM_CAPACITY =1 <<30;
9+
10+
// Current hash-table capacity. Capacity is a power of 2
11+
privateintcapacity;
12+
13+
// Define default load factor
14+
privatestaticfloatDEFAULT_MAX_LOAD_FACTOR =0.5f;
15+
16+
// Specify a load factor used in the hash table
17+
privatefloatloadFactorThreshold;
18+
19+
// The number of entries in the map
20+
privateintsize =0;
21+
22+
// Hash table is an ArrayList
23+
privateArrayList<MyMap.Entry<K,V>>table;
24+
25+
/** Construct a map with the default capacity and load factor */
26+
publicMyHashMap() {
27+
this(DEFAULT_INITIAL_CAPACITY,DEFAULT_MAX_LOAD_FACTOR);
28+
}
29+
30+
/** Construct a map with the specified initial capacity and
31+
* default load factor */
32+
publicMyHashMap(intinitialCapacity) {
33+
this(initialCapacity,DEFAULT_MAX_LOAD_FACTOR);
34+
}
35+
36+
/** Construct a map with the specified initial capacity
37+
* and load factor */
38+
publicMyHashMap(intinitialCapacity,floatloadFactorThreshold) {
39+
if (initialCapacity >MAMIMUM_CAPACITY)
40+
this.capacity =MAMIMUM_CAPACITY;
41+
else
42+
this.capacity =trimToPowerOf2(initialCapacity);
43+
44+
this.loadFactorThreshold =loadFactorThreshold;
45+
table =newArrayList<>();
46+
for (inti =0;i <capacity;i++) {
47+
table.add(null);
48+
}
49+
}
50+
51+
@Override/** Remove all of the entries from this map */
52+
publicvoidclear() {
53+
size =0;
54+
removeEntries();
55+
}
56+
57+
@Override/** Return true if the specified key is in the map */
58+
publicbooleancontainsKey(Kkey) {
59+
if (get(key) !=null)
60+
returntrue;
61+
else
62+
returnfalse;
63+
}
64+
65+
@Override/** Return true if this map contains the value */
66+
publicbooleancontainsValue(Vvalue) {
67+
for (inti =0;i <capacity;i++) {
68+
if (table.get(i) !=null &&
69+
table.get(i).getValue() ==value)
70+
returntrue;
71+
}
72+
73+
returnfalse;
74+
}
75+
76+
@Override/** Return a set of entries in the map */
77+
publicjava.util.Set<Entry<K,V>>entrySet() {
78+
java.util.Set<Entry<K,V>>set =newjava.util.HashSet<>();
79+
80+
for (inti =0;i <capacity;i++) {
81+
if (table.get(i) !=null) {
82+
set.add(table.get(i));
83+
}
84+
}
85+
86+
returnset;
87+
}
88+
89+
@Override/** Return the value that matches the specified key */
90+
publicVget(Kkey) {
91+
inthash1 =hash(key.hashCode());
92+
intindex =hash1;
93+
intj =0;
94+
95+
while (table.get(index) ==null) {
96+
// Secondary hash: (k + j * h'(key)) % N
97+
index =hash1 +j++ *hash2(hash1);
98+
index %=capacity;
99+
}
100+
101+
if (table.get(index).getKey() ==key) {
102+
returntable.get(index).getValue();
103+
}
104+
105+
returnnull;
106+
}
107+
108+
@Override/** Return true if this map contains no entries */
109+
publicbooleanisEmpty() {
110+
returnsize ==0;
111+
}
112+
113+
@Override/** Return a set consisting of the keys in this map */
114+
publicjava.util.Set<K>keySet() {
115+
java.util.Set<K>set =newjava.util.HashSet<>();
116+
117+
for (inti =0;i <capacity;i++) {
118+
if (table.get(i) !=null)
119+
set.add(table.get(i).getKey());
120+
}
121+
122+
returnset;
123+
}
124+
125+
@Override/** Add an entry (key, value) into the map */
126+
publicVput(Kkey,Vvalue) {
127+
inthash1 =hash(key.hashCode());
128+
intindex =hash1;
129+
intj =0;
130+
131+
while (table.get(index) !=null) {
132+
// Add a new entry (key, value)
133+
if (table.get(index).getKey() ==key) {
134+
Entry<K,V>entry =table.get(index);
135+
VoldValue =entry.getValue();
136+
// Replace old value with new value
137+
entry.value =value;
138+
table.set(index,entry);
139+
// Return the old value for the key
140+
returnoldValue;
141+
}
142+
143+
// Secondary hash: (k + j * h'(key)) % N
144+
index =hash1 +j++ *hash2(hash1);
145+
index %=capacity;
146+
}
147+
148+
// Check load factor
149+
if (size >=capacity *loadFactorThreshold) {
150+
if (capacity ==MAMIMUM_CAPACITY)
151+
thrownewRuntimeException("Exceeding maximum capacity");
152+
rehash();
153+
}
154+
155+
// Add a new entry(key, value) to hashtable
156+
table.set(index,newMyMap.Entry<K,V>(key,value));
157+
158+
size++;// Increase size;
159+
160+
returnvalue;
161+
}
162+
163+
@Override/** Remove the entry for the specified key */
164+
publicvoidremove(Kkey) {
165+
inthash1 =hash(key.hashCode());
166+
intindex =hash1;
167+
intj =0;
168+
169+
while (table.get(index) ==null) {
170+
// Secondary hash: (k + j * h'(key)) % N
171+
index =hash1 +j++ *hash2(hash1);
172+
index %=capacity;
173+
}
174+
175+
if (table.get(index).getKey() ==key) {
176+
table.remove(index);
177+
}
178+
}
179+
180+
@Override/** Return the number of entries in this map */
181+
publicintsize() {
182+
returnsize;
183+
}
184+
185+
@Override/** Return a set consisting of values in this map */
186+
publicjava.util.Set<V>values() {
187+
java.util.Set<V>set =newjava.util.HashSet<>();
188+
189+
for (inti =0;i <capacity;i++) {
190+
if (table.get(i) !=null)
191+
set.add(table.get(i).getValue());
192+
}
193+
194+
returnset;
195+
}
196+
197+
/** Hash function */
198+
privateinthash(inthashCode) {
199+
returnsupplementalHash(hashCode) & (capacity -1);
200+
}
201+
202+
/** Ensure the hashing is evenly distributed */
203+
privatestaticintsupplementalHash(inth) {
204+
h ^= (h >>>20) ^ (h >>>12);
205+
returnh ^ (h >>>7) ^ (h >>>4);
206+
}
207+
208+
/** Secondary hash function */
209+
privateinthash2(inthash) {
210+
return (7 -hash) & (7 -1);
211+
}
212+
213+
/** Return a power of 2 for initialCapacity */
214+
privateinttrimToPowerOf2(intinitialCapacity) {
215+
intcapacity =1;
216+
while (capacity <initialCapacity) {
217+
capacity <<=1;
218+
}
219+
220+
returncapacity;
221+
}
222+
223+
/** Remove all entries from map */
224+
privatevoidremoveEntries() {
225+
table.clear();
226+
}
227+
228+
/** Rehash the map */
229+
privatevoidrehash() {
230+
capacity <<=1;// Same as capacity *= 2. <= is more efficient
231+
for (inti =size +1;i <capacity;i++) {
232+
table.add(null);
233+
}
234+
}
235+
236+
@Override/** Return a string repersentation of this map */
237+
publicStringtoString() {
238+
StringBuilderbuilder =newStringBuilder("[");
239+
240+
for (Entry<K,V>entry :table) {
241+
if (entry !=null &&table.size() >0) {
242+
builder.append(entry);
243+
}
244+
}
245+
246+
builder.append("]");
247+
returnbuilder.toString();
248+
}
249+
}
970 Bytes
Binary file not shown.
818 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp