hashmap的基本方法
//创建map对象
HashMap<Object, Object> map = new HashMap<>();
map.put(1,12);
map.put(2,13);
map.put(3,12);
System.out.println(map);
System.out.println("++++++++++++++++++++++++");
//public V remove(Object key) : 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
System.out.println(map.remove(2));
System.out.println(map);
System.out.println("+++++++++++++++++++++++++");
//public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
System.out.println(map.get(3));
System.out.println("++++++++++++++++++++++++++");
``
``
//boolean containsKey(Object key) 判断集合中是否包含指定的键。
System.out.println(map.containsKey(2));
System.out.println(map.containsKey(3));
System.out.println("++++++++++++++++++++++++++++");
//public Set<K> keySet() : 获取Map集合中所有的键,存储到Set集合中。
System.out.println(map.keySet());
System.out.println("+++++++++++++++++++++++++++++");
System.out.println(map.entrySet());
System.out.println("++++++++++++++++++++++++++++++");
//使⽤put⽅法时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到集合中;
//若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的值,替换成指定的新值
//获取所有的键 获取键集
Set<Object> key = map.keySet();
for (Object o : key) {
//o 就是健值
//获取对应值
map.get(o);
System.out.println(map.get(o));
// 获取 所有的 entry
Set<Map.Entry<Object, Object>> setentry = map.entrySet();
for (Map.Entry<Object, Object> o : setentry) {
System.out.println(o.getKey());
System.out.println(o.getValue());
}`
结果
{1=12, 2=13, 3=12}
++++++++++++++++++++++++
13
{1=12, 3=12}
+++++++++++++++++++++++++
12
++++++++++++++++++++++++++
false
true
++++++++++++++++++++++++++++
[1, 3]
+++++++++++++++++++++++++++++
[1=12, 3=12]
++++++++++++++++++++++++++++++
12
12
1
12
3
12