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();

 

posted @ 2019-08-16 16:27  不咬人的兔子  阅读(140)  评论(0编辑  收藏  举报