java-hashmap

hashmap虽说网上各种高大上,不过个人感觉就是一个个人可以定义下标(key)的数组

一般两部分组成,key和value

声明一个hashmap:

HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();   <Integer>也可以用<String>等

需要导入的包:

import java.util.HashMap;
import java.util.Map;

获取对应value:

hash.get(key);

获取对应value,如果没有返回默认值default:

hash.getOrDefault(key,default);

存入数据

hash.put(key,value);

把map hash2存入map hash1

hash1.putAll(hash2);

删除数据:

hash.remove(key);

hash.remove(key,value);

修改value:

hash.replace(key, newvalue);

hash.replace(key, oldvalue, newvalue);

判断是否存在key

hash.containsKey(key)

清空

hash.clear();

数据个数:

hash.size();

原文地址:https://www.cnblogs.com/clamp7724/p/11364659.html