forked from RoWe98/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashMap.java
More file actions
24 lines (24 loc) · 890 Bytes
/
MyHashMap.java
File metadata and controls
24 lines (24 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
//定义一个HashMapSon类,它继承HashMap类
class myHashMap<K, V> extends HashMap<K,V> {
private static final long serialVersionUID = -5894887960346129860L;
// 重写HashMapSon类的toString()方法
@Override
public String toString() {
Set<Map.Entry<K, V>> keyset = this.entrySet();
Iterator<Map.Entry<K, V>> i = keyset.iterator();
if (!i.hasNext())
return "";
StringBuffer buffer = new StringBuffer();
buffer.append("{");//注意此程序与源代码的区别
for (;;) {
Map.Entry<K, V> me = i.next();
K key = me.getKey();
V value = me.getValue();
buffer.append(key.toString() + ":");
buffer.append(value.toString() + "\n");
if (!i.hasNext())
return buffer.append("}").toString();
}
}
}