Java HashMap集合的用法(附带实例)
HashMap 是 Map 接口中使用频率最高的实现类。Map 接口结构如下图所示。

图 1 Map接口结构
与 HashSet 一样,HashMap 元素的存取顺序不能保证一致。HashMap 通常使用 Map 接口的方法,下面介绍一些简单的方法。
【实例】创建集合,实现 HashMap 的简单使用方法,代码如下:
HashMap 判断两个 key 相等的标准是,两个 key 的哈希值相等,通过 equals() 方法返回 true。HashMap 判断两个 value 相等的标准是,两个 value 通过 equals() 方法返回 true。

图 1 Map接口结构
HashMap 是线程不安全的,允许添加 null 键和 null 值。HashMap 存储数据采用的是哈希表结构,底层使用一维数组+单向链表+红黑树对 key-value 对数据进行存储。在双列集合中,Map 接口与 Collection 接口并列存在,用于保存具有映射关系的数据:key-value 对数据。Collection 接口的元素是孤立存在的,Map 接口的元素是成对存在的。Map 接口中的 key 和 value 都可以是任何引用类型的数据。
与 HashSet 一样,HashMap 元素的存取顺序不能保证一致。HashMap 通常使用 Map 接口的方法,下面介绍一些简单的方法。
HashMap常用方法
1) 添加、修改操作
- Object put(Object key, Object value): 将指定 key-value 对数据添加到(或修改)当前 HashMap 对象中。
- void putAll(Map m): 将 m 中的所有 key-value 对数据存放在当前 HashMap 对象中。
2) 元素查询的操作
- Object get(Object key): 获取指定 key 对应的 value。
- boolean containsKey(Object key): 是否包含指定的 key。
- boolean containsValue(Object value): 是否包含指定的 value。
- int size(): 返回 Map 中 key-value 对的个数。
- boolean isEmpty(): 判断当前 Map 是否为空。
3) 元视图操作的方法
- Set keySet(): 返回所有 key 构成的集合。
- Collection values(): 返回所有 value 构成的集合。
- Set entrySet(): 返回所有 key-value 对数据构成的集合。
4) 删除操作
- Object remove(Object key): 根据指定 key 移除对应的 key-value 对数据,并返回 value。
- void clear(): 清空当前 Map 中的所有数据。
【实例】创建集合,实现 HashMap 的简单使用方法,代码如下:
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
MapDemo mapDemo = new MapDemo();
mapDemo.testMap();
}
public void testMap(){
System.out.println("===========添加元素===========");
HashMap hashMap = new HashMap();
hashMap.put("a", "student01");
hashMap.put("b", "student02");
hashMap.put("c", "student03");
hashMap.put("d", "student04");
hashMap.put("d", "student05");
System.out.println(hashMap);
HashMap hashMap02 = new HashMap();
hashMap02.put("test", "hashMap02");
hashMap.putAll(hashMap02);
System.out.println(hashMap);
System.out.println("===========查询元素===========");
Object o = hashMap.get("a");
System.out.println(o);
boolean b = hashMap.containsKey("a");
System.out.println(b);
boolean b1 = hashMap.containsValue("student01");
System.out.println(b1);
int size = hashMap.size();
System.out.println(size);
boolean empty = hashMap.isEmpty();
System.out.println(empty);
System.out.println("===========查询元素===========");
Object a = hashMap.remove("a");
System.out.println(a);
System.out.println("===========元视图操作===========");
Set set = hashMap.keySet();
System.out.println(set);
Collection values = hashMap.values();
System.out.println(values);
Set set1 = hashMap.entrySet();
System.out.println(set1);
System.out.println("===========删除元素===========");
hashMap.clear();
System.out.println(hashMap);
}
}
运行结果为:
===========添加元素===========
{a=student01, b=student02, c=student03, d=student05}
{a=student01, b=student02, c=student03, d=student05, test=hashMap02}
===========查询元素===========
student01
true
true
5
false
===========查询元素===========
student01
===========元视图操作===========
[b, c, d, test]
[student02, student03, student05, hashMap02]
[b=student02, c=student03, d=student05, test=hashMap02]
===========删除元素===========
{}
在上述代码中,我们使用的方法都是 Map 接口的方法,实现了添加和修改元素、获取元素、查询元素、删除元素,以及对元视图的操作。在 Map 接口中没有修改方法,对元视图的操作可以用在集合遍历中。HashMap 判断两个 key 相等的标准是,两个 key 的哈希值相等,通过 equals() 方法返回 true。HashMap 判断两个 value 相等的标准是,两个 value 通过 equals() 方法返回 true。
ICP备案:
公安联网备案: