Java基础-集合框架-Map-HashMap API及常用方法

HashMap

HashMap API

Modifier and TypeMethod and Description
voidclear()
清除HashMap中的所有元素。
Objectclone()
返回此HashMap中的浅拷贝:其内元素不会被拷贝。
Vcompute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
根据key和函数表达式设置新的value,并返回新的value.当新的value为null时,删除对应的key。
VcomputeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
key存在时不进行操作,不存在时新增元素,value为null时不进行操作 。
VcomputeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
key存在且相关联的 value不为 null时,函数根据 key对应的value计算出newValue,使用newValue替换旧值。
booleancontainsKey(Object key)
如果此HashMap中包含指定key,则返回 true 。
booleancontainsValue(Object value)
如果此HashMap中包含指定value,则返回 true 。
Set<Map.Entry<K,V>>entrySet()
遍历此HashMap中的所有元素。
voidforEach(BiConsumer<? super K,? super V> action)
遍历操作此HashMap中的所有元素。
Vget(Object key)
根据key返回对应的value
VgetOrDefault(Object key, V defaultValue)
根据key返回对应的value,如果对应value为null,返回defaultValue
booleanisEmpty()
如果此地图不包含键值映射,则返回 true 。
SetkeySet()
判断此HashMap是否为空。
Vmerge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
如果指定的key尚未与value相关联或与null相关联,则将其与给定的非空值相关联。
Vput(K key, V value)
增加元素。
voidputAll(Map<? extends K,? extends V> m)
将另一HashMap中的所有元素添加到此HashMap中。
VputIfAbsent(K key, V value)
如果指定的键尚未与某个值相关联(或映射到 null ),则将其与给定值相关联并返回。
Vremove(Object key)
根据key移除元素。
booleanremove(Object key, Object value)
根据key-value移除指定元素。
Vreplace(K key, V value)
根据key替换value。
booleanreplace(K key, V oldValue, V newValue)
根据key-oldvalue为条件 替换oldvalue为newValue。
voidreplaceAll(BiFunction<? super K,? super V,? extends V> function)
根据条件对所有value进行替换。
intsize()
返回此HashMap中元素的数量。
Collectionvalues()
遍历返回此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);
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值