①特性
- 非线程安全
- hashMap的映射不是有序的
- key、value都可以为null
②常用方法
- get(Object key)
- put(Object key,Object value)
- remove(Object key)
- remove(Object key,Object value)
- isEmpty()
- size()
- clone()
- clear()
- entrySet()
- keySet()
- values()
- containsKey(Object key)
- containsValue(Object value)
- getOrDefault(Object key,Object defaultValue)
- replace(Object key,Object oldValue,Object newValue)
- replace(Object key,Object value)
- replaceAll(BigFunction function)
- putAll(Map m)
- putIfAbsent(Object key,Object value)
- compute(Objetc key,BigFunction bf)
- computeIfAbsent(Object key,Function f)
- computeIfPresent(Object key,BigFunction bf)
- merge(Object key,Object value,BigFunction bf)
get/put/remove/isEmpty/size
@Test
public void test() {
HashMap<String, Integer> map = new HashMap();
map.put("key1", 1);
map.put("key2", 2);
System.out.println(map);
System.out.println("key1 的值" + map.get("key1"));
map.remove("key1");
map.remove("key2", 3);
System.out.println(map);
System.out.println(map.isEmpty());
System.out.println("map size" + map.size());
map.remove("key2", 2);
System.out.println(map.isEmpty());
}
clone/clear
public void test2() {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);
HashMap<String, Integer> cloneMap = (HashMap) map.clone();
System.out.println(map);
System.out.println(cloneMap);
cloneMap.remove("key1");
System.out.println(map);
System.out.println(cloneMap);
map.clear();
cloneMap.clear();
System.out.println(map);
System.out.println(cloneMap);
}
entrySet/keySet/values
@Test
public void test3() {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
for (String key : map.keySet()) {
System.out.println(key);
}
for (Integer value : map.values()) {
System.out.println(value);
}
}
containsKey/containsValue
@Test
public void test4() {
HashMap<String, Student> map = new HashMap<>();
Student ryan = new Student(18);
map.put("Ryan", ryan);
System.out.println(map.containsKey("Ryan"));
System.out.println(map.containsKey("ryan"));
System.out.println(map.containsValue(ryan));
}
getOrDefault
@Test
public void test5() {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
System.out.println(map.get("key1"));
Integer n = map.getOrDefault("key1", 2);//map 中存在key = "key1" 返回对应的值,否则返回 2
Integer m = map.getOrDefault("key2",2);
System.out.println(n);
System.out.println(m);
}
replace
@Test
public void test6() {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);
System.out.println(map);
map.replace("key1", 1, 4);
System.out.println(map);
map.replace("key2", 5);
System.out.println(map);
//replaceAll 对集合中的键值对进行计算
map.replaceAll((k, v) -> {
System.out.println(k);
System.out.println(v);
return null;
});
}
putAll/putIfAbsent
@Test
public void test7() {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
HashMap<String, Integer> map2 = new HashMap<>();
map.put("key2", 2);
map.putAll(map2);
System.out.println(map);
map.putIfAbsent("key1", 2);
map.putIfAbsent("key3", 3);
System.out.println(map);
}
merge/computeIfAbsent
https://blog.csdn.net/mengxb12138/article/details/80893937