Java中定义HashMap

java中如何定义HashMap

//实例化HashMap
Map <Integer,Integer> map = new HashMap<>();

这里举例如何将数组存入HashMap中

// 定义一个int型数组
int []arr = new int[]{1,2,3};
// for循环
for(int cur = 0,temp;cur<arr.length;cur++){
    temp = arr[cur];
    map.put(temp,cur);
}
// 判断HashMap中是否有当前元素,返回boolean类型
map.containsKey(value);//value为待判断元素
// 获取指定元素下标
map.get(value);
// 清空HashMap
map.clear();

以上为做题总结,欢迎点评


版权声明:本文为Galileoh原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。