HashMap
概述
基于哈希表的 Map
接口的实现。此实现提供所有可选的映射操作,并允许使用 null
值和 null
键。(除了非同步和允许使用 null 之外,HashMap
类与 Hashtable
大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
HashMap 数据结构为 数组+链表,其中:链表的节点存储的是一个 Entry 对象,每个Entry 对象存储四个属性(hash,key,value,next)
API方法示例
clear
@Test
public void clear() {
//clear: 清除map集合所有键值
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"clear");
//调用clear方法
hashMap.clear();
//此时打印为空
System.out.println("hashMap = " + hashMap);
}
Clone
@Test
public void Clone() {
//clone: 拷贝返回集合的数据.
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"clone");
Object clone = hashMap.clone();
//打印为集合里的键值数据
System.out.println("clone = " + clone);
}
compute
@Test
public void compute() {
//compute: 计算指定键映射的值,如果不存在打印为null
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"compute + ");
//存在1这个键值,打印集合数据加方法指定的数据
hashMap.compute(1, (k, v) -> (v == null) ? "null" : v.concat("hashMap"));
System.out.println("map = " + hashMap);
}
computeIfAbsent
@Test
public void computeIfAbsent() {
//computeIfAbsent: 如果不存在指定的键则输出设置的值
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"computeIfAbsent");
//没有2这个key,则输出右边设置的值
System.out.println(hashMap.computeIfAbsent(2, key -> "without this key"));
}
computeIfPresent
@Test
public void computeIfPresent() {
//computeIfPresent: 如果指定的键存在则设置一个新的值,否则为null
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"computeIfPresent");
//存在1这个键,利用参数替换集合的value
System.out.println(hashMap.computeIfPresent(1, (k,v) -> "new element"));
}
containsKey
@Test
public void containsKey() {
//containsKey: 判断是否包含指定的key,不包含返回false
HashMap<Integer, String> hashMap = new HashMap<>();
//没有1这个key,打印为false
System.out.println("map.containsKey(1) = " + hashMap.containsKey(1));
}
containsValue
@Test
public void containsValue() {
//判断是否包含指定的值,不包含则返回false
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"not data");
//不存在这个指定的值,返回为false
System.out.println(hashMap.containsValue("containsValues"));
}
entrySet
@Test
public void entrySet() {
//entrySet: 返回集合的所有数据
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"entrySet");
Set<Map.Entry<Integer, String>> entrySet = hashMap.entrySet();
//使用增强for循环遍历key和value
for (Map.Entry<Integer, String> entry : entrySet) {
System.out.println("键"+entry.getKey() + ":" + "值" + entry.getValue());
}
}
forEach
@Test
public void forEach() {
//forEach: 对集合进行遍历
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"forEach method");
//使用lambda表达式对集合进行遍历操作
hashMap.forEach((K,V) -> System.out.println(V));
}
get
@Test
public void get() {
//get: 根据key获取value
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"get method");
System.out.println("hashMap.get(1) = " + hashMap.get(1));
}
getOrDefault
@Test
public void getOrDefault() {
//getOrDefault: 返回指定key的值,如果不存在该key则返回defaultValue的值
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"getOrDefault method");
//不存在3号key,返回参数里指定的数据
System.out.println(hashMap.getOrDefault(3, "getOrDefault"));
}
isEmpty
@Test
public void isEmpty() {
//isEmpty: 判断集合是否为空,空则返回true
HashMap<Integer, String> hashMap = new HashMap<>();
System.out.println("hashMap.isEmpty() = " + hashMap.isEmpty());
}
keySet
@Test
public void keySet() {
//keySet: 返回集合的key值
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(100,"KeySet");
System.out.println("map.keySet() = " + hashMap.keySet());
}
merge
@Test
public void merge() {
//marge: 如果指定的键不存在则输出设置的value值,如果存在则用存在键中的值加merge参数里的值
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"marge");
System.out.println(hashMap.merge(1, " + new Value", (k, v) -> (k + v)));
}
put
@Test
public void put() {
//put: 设置一个键值对数据,键不可重复
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"put1");
hashMap.put(2,"put2");
Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
//使用entrySet方法遍历map集合
for (HashMap.Entry<Integer, String> entry : entries) {
String kv = "Key = " + entry.getKey() + " " + "Value = " + entry.getValue();
System.out.println(kv);
}
}
putAll
@Test
public void putAll() {
//putAll: 根据一个map集合批量添加参数
HashMap<Integer, String> hashMap1 = new HashMap<>();
HashMap<Integer, String> hashMap2 = new HashMap<>();
hashMap2.put(1,"putAll method");
hashMap1.putAll(hashMap2);
//打印map1集合
System.out.println(hashMap1);
}
putIfAbsent
@Test
public void putIfAbsent() {
//putIfAbsent: 如果存在指定的键则返回map集合的数据,否则返回null
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"putIfAbsent");
System.out.println(hashMap.putIfAbsent(1, "putIfAbsent"));
}
remove
@Test
public void remove() {
//remove: 移除指定的key
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"remove method");
//移除1号key
System.out.println("map.remove(1) = " + hashMap.remove(1));
}
remove(Object key, Object value)
@Test
public void removeTwoArgs() {
//remove(Object key, Object value) :移除指定的键值对
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"remove method");
//移除指定的键值对
System.out.println(hashMap.remove(1, "remove method"));
}
replace
@Test
public void replace() {
//replace: 如果存在指定的key则替换新的value
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"replace");
//替换键1的值为new Value
System.out.println(hashMap.replace(1, "new Value"));
//此时打印为替换的字符串
System.out.println(hashMap);
}
replace(K key, V oldValue, V newValue)
@Test
public void replaceThreeArgs() {
//replace(K key, V oldValue, V newValue): 根据key和旧value替换新的value
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"replace");
//指定旧value和新value
System.out.println(hashMap.replace(1, "replace", "new replace"));
System.out.println("map = " + hashMap);
}
replaceAll
@Test
public void replaceAll() {
//replaceAll: 通过lambda批量添加
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"replaceAll");
hashMap.replaceAll((k,v) -> "new" + "replaceAll" + "Lambda");
System.out.println("map = " + hashMap);
}
size
@Test
public void size() {
//size: 返回集合数据长度
HashMap<Integer, String> hashMap = new HashMap<>();
System.out.println("hashMap.size() = " + hashMap.size());
}
values
@Test
public void values() {
//values: 返回集合所有数据
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1,"values");
hashMap.put(2,"values");
System.out.println("hashMap.values() = " + hashMap.values());
}