本文仅供参考!!!
Map<String, String> map = new HashMap<String, String>();
map.put("apple", "苹果"); //向列表中添加数据
map.put("computer", "计算机"); //向列表中添加数据
map.put("book", "书"); //向列表中添加数据
map.put("book", "");
//遍历map中的键
for (String key : map.keySet()) {
System.out.println("map的key= "+ key +" map的 value= " + map.get(key));
}
打印结果:
map的key= apple map的 value= 苹果
map的key= computer map的 value= 计算机
map的key= book map的 value=
Process finished with exit code 0
结果:
20210608补充拓展
public class test2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("book", "书");
map.put("car", "aodi");
System.out.println("============================第一次set的map对象");
System.out.println(ClassLayout.parseInstance(map).toPrintable());
System.out.println("===========================book对象");
System.out.println(ClassLayout.parseInstance(map.get("book")).toPrintable());
System.out.println(ClassLayout.parseInstance(map.get("car")).toPrintable());
System.out.println("===========================book=set空串儿");
map.put("book", "");
map.put("car", "");
System.out.println(ClassLayout.parseInstance(map.get("book")).toPrintable());
System.out.println(ClassLayout.parseInstance(map.get("car")).toPrintable());
}
}
结果:
只打印map对象:
============================第一次set的map对象
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
java.util.HashMap object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 37 00 f8 (10101000 00110111 00000000 11111000) (-134203480)
12 4 java.util.Set AbstractMap.keySet null
16 4 java.util.Collection AbstractMap.values null
20 4 int HashMap.size 2
24 4 int HashMap.modCount 2
28 4 int HashMap.threshold 12
32 4 float HashMap.loadFactor 0.75
36 4 java.util.HashMap.Node[] HashMap.table [null, null, null, null, null, (object), null, (object), null, null, null, null, null, null, null, null]
40 4 java.util.Set HashMap.entrySet null
44 4 (loss due to the next object alignment)
Instance size: 48 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
===========================book对象
java.lang.String object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) da 02 00 f8 (11011010 00000010 00000000 11111000) (-134216998)
12 4 char[] String.value [书]
16 4 int String.hash 0
20 4 (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.String object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) da 02 00 f8 (11011010 00000010 00000000 11111000) (-134216998)
12 4 char[] String.value [a, o, d, i]
16 4 int String.hash 0
20 4 (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
===========================book=set空串儿
java.lang.String object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) da 02 00 f8 (11011010 00000010 00000000 11111000) (-134216998)
12 4 char[] String.value []
16 4 int String.hash 0
20 4 (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.String object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) da 02 00 f8 (11011010 00000010 00000000 11111000) (-134216998)
12 4 char[] String.value []
16 4 int String.hash 0
20 4 (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total