Skip to content

Commit 1debf49

Browse files
committed
HashMap coding error
1 parent 252cb9a commit 1debf49

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

sources/net.sf.j2s.java.core/src/java/util/HashMap.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public HashMap(Map<? extends K, ? extends V> m) {
497497
}
498498

499499
/**
500-
* Implements Map.putAll and Map constructor
500+
* Implements clone, Map.putAll and Map constructor
501501
*
502502
* @param mOriginal the map
503503
* @param evict false when initially constructing this map, else true (relayed
@@ -541,8 +541,11 @@ void putMapEntries(Map<? extends K, ? extends V> mOriginal, boolean evict) {
541541
return;
542542
}
543543
秘m = null;
544+
544545
for (Map.Entry<? extends K, ? extends V> e : mOriginal.entrySet()) {
545-
putVal(hash(key), e.getKey(), e.getValue(), false, evict, NOT_SIMPLE);
546+
key = e.getKey();
547+
value = e.getValue();
548+
putVal(hash(key), key, value, false, evict, NOT_SIMPLE);
546549
}
547550

548551
}

sources/net.sf.j2s.java.core/src/test/Test_Map.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map.Entry;
1111
import java.util.Set;
1212
import java.util.Spliterator;
13+
import java.util.TreeMap;
1314
import java.util.function.BiConsumer;
1415
import java.util.function.BiFunction;
1516
import java.util.function.Consumer;
@@ -97,6 +98,9 @@ public static void main(String[] args) {
9798
System.out.println(System.identityHashCode(new String("test")));
9899
System.out.println(System.identityHashCode(new String("test")));
99100
System.out.println(System.identityHashCode(new String("test")));
101+
102+
testMap();
103+
100104
testSets();
101105

102106
testIdentity();
@@ -107,6 +111,46 @@ public static void main(String[] args) {
107111
System.out.println("Test_Map OK");
108112
}
109113

114+
private static void testMap() {
115+
LinkedHashMap<String, String> hm = new LinkedHashMap<>();
116+
hm.put("key1", "v1");
117+
hm.put("key3", "v3");
118+
hm.put("key2", "v2");
119+
120+
LinkedHashMap<String, String> hm1 = (LinkedHashMap<String, String>) hm.clone();
121+
122+
assert(hm1.get("key1") == "v1");
123+
assert(hm1.get("key2") == "v2");
124+
assert(hm1.get("key3") == "v3");
125+
126+
for (String k : hm.keySet()) {
127+
System.out.println(k);
128+
}
129+
130+
131+
LinkedHashMap<Object, String> hm2 = new LinkedHashMap<>();
132+
hm2.put(new int[] {1,2,3}, "[123]");
133+
hm2.put(new int[] {2, 3, 4}, "[234]");
134+
System.out.println(hm2.get(new int[] {1,2,3}));
135+
136+
TreeMap<String, String> hm3 = new TreeMap<>();
137+
hm3.put("key1", "v1");
138+
hm3.put("key3", "v3");
139+
hm3.put("key2", "v2");
140+
141+
assert(hm3.get("key1") == "v1");
142+
assert(hm3.get("key2") == "v2");
143+
assert(hm3.get("key3") == "v3");
144+
145+
String k0 = "key0";
146+
for (String k : hm3.keySet()) {
147+
System.out.println(k);
148+
assert (k0.compareTo(k) == -1);
149+
k0 = k;
150+
}
151+
152+
}
153+
110154
class MySet {
111155

112156
public int hashCode() {

0 commit comments

Comments
 (0)