HashMap
HashMap API
Modifier and Type | Method and Description |
---|---|
void | clear() 清除HashMap中的所有元素。 |
Object | clone() 返回此HashMap中的浅拷贝:其内元素不会被拷贝。 |
V | compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 根据key和函数表达式设置新的value,并返回新的value.当新的value为null时,删除对应的key。 |
V | computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) key存在时不进行操作,不存在时新增元素,value为null时不进行操作 。 |
V | computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) key存在且相关联的 value不为 null时,函数根据 key对应的value计算出newValue,使用newValue替换旧值。 |
boolean | containsKey(Object key) 如果此HashMap中包含指定key,则返回 true 。 |
boolean | containsValue(Object value) 如果此HashMap中包含指定value,则返回 true 。 |
Set<Map.Entry<K,V>> | entrySet() 遍历此HashMap中的所有元素。 |
void | forEach(BiConsumer<? super K,? super V> action) 遍历操作此HashMap中的所有元素。 |
V | get(Object key) 根据key返回对应的value |
V | getOrDefault(Object key, V defaultValue) 根据key返回对应的value,如果对应value为null,返回defaultValue |
boolean | isEmpty() 如果此地图不包含键值映射,则返回 true 。 |
Set | keySet() 判断此HashMap是否为空。 |
V | merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) 如果指定的key尚未与value相关联或与null相关联,则将其与给定的非空值相关联。 |
V | put(K key, V value) 增加元素。 |
void | putAll(Map<? extends K,? extends V> m) 将另一HashMap中的所有元素添加到此HashMap中。 |
V | putIfAbsent(K key, V value) 如果指定的键尚未与某个值相关联(或映射到 null ),则将其与给定值相关联并返回。 |
V | remove(Object key) 根据key移除元素。 |
boolean | remove(Object key, Object value) 根据key-value移除指定元素。 |
V | replace(K key, V value) 根据key替换value。 |
boolean | replace(K key, V oldValue, V newValue) 根据key-oldvalue为条件 替换oldvalue为newValue。 |
void | replaceAll(BiFunction<? super K,? super V,? extends V> function) 根据条件对所有value进行替换。 |
int | size() 返回此HashMap中元素的数量。 |
Collection | values() 遍历返回此HashMap中所有元素的value。 |
Sample Code
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
private static HashMap<String, String> hashMap = new HashMap<String, String>();
static {
hashMap.put("A", "a");
hashMap.put("B", "b");
hashMap.put("C", "c");
hashMap.put("D", "d");
hashMap.put("E", "e");
}
public static void main(String[] args) {
values();
}
/**
* @Function: compute
* @Description: 根据key和函数表达式设置新的value,并返回新的value.当新的value为null时,删除对应的key
*
* @param hashMap
* @param paramKey
*/
public static void compute() {
String newValue = hashMap.compute("A", (key, value) -> "newValue=A");
// newValue=A
System.out.println(newValue);
// key不存在时新增
hashMap.compute("F", (key, value) -> "new F");
// E的value为null,所以删除元素
hashMap.compute("E", (key, value) -> null);
/*
* A:newValue=A
* B:b
* C:c
* D:d
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: computeIfAbsent
* @Description: key存在时不进行操作,不存在时新增元素,value为null时不进行操作.
*
* @param hashMap
*/
public static void computeIfAbsent() {
String newValue = hashMap.computeIfAbsent("F", key -> "f");
// f
System.out.println(newValue);
// key存在,不进行操作
hashMap.computeIfAbsent("A", key -> "new a");
// value为null,不进行操作
hashMap.computeIfAbsent("G", key -> null);
/*
* A:a
* B:b
* C:c
* D:d
* E:e
* F:f
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: computeIfPresent
* @Description: key存在且相关联的 value不为 null时,函数根据 key对应的value计算出newValue,使用newValue替换旧值.
*
* @param hashMap
*/
public static void computeIfPresent() {
String newValue = hashMap.computeIfPresent(
"A", (key, value) -> "new" + value
);
// newa
System.out.println(newValue);
// 不进行操作
hashMap.computeIfPresent("F",(key, value) -> null);
// 不进行操作
hashMap.computeIfPresent("F",(key, value) -> "new F");
/*
* A:newa
* B:b
* C:c
* D:d
* E:e
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: entrySet
* @Description: 遍历HashMap中所有元素
*
* @param hashMap
*/
public static void entrySet() {
/*
* A:a
* B:b
* C:c
* D:d
* E:e
*/
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
/**
* @Function: forEach
* @Description: 遍历HashMap中所有元素
*
* @param hashMap
*/
public static void forEach() {
/*
* A:a
* B:b
* C:c
* D:d
* E:e
*/
hashMap.forEach(
(key, value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: keySet
* @Description: 遍历HashMap中的所有key
*
* @param hashMap
*/
public static void keySet() {
/*
* A
* B
* C
* D
* E
*/
for(String key : hashMap.keySet()) {
System.out.println(key);
}
}
/**
* @Function: merge
* @Description: 根据key获得value并和新的参数value合并
* oldVal 为根据key获取的value , newValue 为 newVal , put(key,oldVal + newVal)
*
* @param hashMap
* @param key 需要操作的map的key
* @param newValue 需要合并的值
*/
public static void merge() {
String newValue = hashMap.merge("A", "新增拼接字符串", (oldVal, newVal) -> oldVal + newVal);
// a新增拼接字符串
System.out.println(newValue);
// value为null时删除键值对
hashMap.merge("A", "新增拼接字符串", (oldVal, newVal) -> null);
// 不存在时新增
hashMap.merge("F", "新增拼接字符串", (oldVal, newVal) -> oldVal + newVal);
/*
* B:b
* C:c
* D:d
* E:e
* F:新增拼接字符串
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: putIfAbsent
* @Description: key不存在或对应的value为null时,进行新增操作
*
* @param hashMap
* @param key
* @param value
*/
public static void putIfAbsent() {
// key = A 存在,且value不为null
hashMap.putIfAbsent("A", "new a");
// value为null时,可进行新增
hashMap.put("F", null);
hashMap.putIfAbsent("F", "new f");
/*
* A:a
* B:b
* C:c
* D:d
* E:e
* F:new f
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: replaceAll
* @Description: 对HashMap中所有元素的value按条件进行替换
*
* @param hashMap
*/
public static void replaceAll() {
hashMap.replaceAll(
(key,value) -> {
if(key.equals("A")) {
return "a";
}else {
return "B";
}
}
);
/*
* A:a
* B:B
* C:B
* D:B
* E:B
*/
hashMap.forEach(
(key,value) -> {
System.out.println(key +":"+ value);
}
);
}
/**
* @Function: values
* @Description: 遍历HashMap中所有元素的value
*
* @param hashMap
*/
public static void values() {
/*
* a
* b
* c
* d
* e
*/
for(String value : hashMap.values()) {
System.out.println(value);
}
}
}