package test;
import java.util.HashMap;
public class TestHashMap {
public static void main(String[] args) {
HashMap<Integer,String> stringMap = new HashMap<Integer,String>();
// put(key,value)方法:向map中追加元素
stringMap.put(1, "java");
stringMap.put(2, "C++");
stringMap.put(3, "C");
System.out.println("执行put(key,value)方法后:" + stringMap);
System.out.println("===============================");
// size()方法:返回映射中的键-值映射关系数量
System.out.println("size()方法:" + stringMap.size());
System.out.println("===============================");
// isEmpty()方法:判断map是否为空,空=》true,非空=》false
System.out.println("isEmpty()方法:" + stringMap.isEmpty());
System.out.println("===============================");
System.out.println("get(key)方法:" + stringMap.get(1));
System.out.println("===============================");
//containsKey(Key)方法:包含Key返回true;不包含Key返回false
System.out.println("containsKey()方法:" + stringMap.containsKey(1));
System.out.println("===============================");
//putAll(Map<? extends K,? extends V> m)方法:这些映射关系将替换此映射目前针对指定映射的所有键的所有映射关系
HashMap<Integer,String> newMap = new HashMap<Integer,String>();
newMap.put(1, "hahaha");
newMap.put(2, "wuwuwu");
stringMap.putAll(newMap);
System.out.println("putAll()方法:"+ stringMap);
System.out.println("===============================");
System.out.println("执行remove(Key)方法前:" + stringMap);
//remove(Key)方法:移除指定位置的数据
stringMap.remove(3);
System.out.println("执行remove(Key)方法后:" + stringMap);
System.out.println("===============================");
//clone()方法:
Object cloneObj = stringMap.clone();
System.out.println("clone()方法::" + cloneObj);
System.out.println("===============================");
//keySet()方法:返回此映射所包含的键的 set 视图
System.out.println("keySet()方法:" + stringMap.keySet());
System.out.println("===============================");
//values():返回此映射所包含的值的 collection 视图
System.out.println("values()方法:" + stringMap.values());
System.out.println("===============================");
//entrySet()方法:返回此映射所包含的映射关系的 collection 视图
System.out.println("entrySet()方法:" + stringMap.entrySet());
System.out.println("===============================");
//clear()方法:从此映射中移除所有映射关系
stringMap.clear();
System.out.println("clear()方法:" + stringMap);
}
}
运行结果:
执行put(key,value)方法后:{1=java, 2=C++, 3=C}
===============================
size()方法:3
===============================
isEmpty()方法:false
===============================
get(key)方法:java
===============================
containsKey()方法:true
===============================
putAll()方法:{1=hahaha, 2=wuwuwu, 3=C}
===============================
执行remove(Key)方法前:{1=hahaha, 2=wuwuwu, 3=C}
执行remove(Key)方法后:{1=hahaha, 2=wuwuwu}
===============================
clone()方法::{2=wuwuwu, 1=hahaha}
===============================
keySet()方法:[1, 2]
===============================
values()方法:[hahaha, wuwuwu]
===============================
entrySet()方法:[1=hahaha, 2=wuwuwu]
===============================
clear()方法:{}
KeySet =》java源码:
//java源码
public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null ? ks : (keySet = new KeySet()));
}
private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return newKeyIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsKey(o);
}
public boolean remove(Object o) {
return HashMap.this.removeEntryForKey(o) != null;
}
public void clear() {
HashMap.this.clear();
}