Map的实现类HashMap的常用方法及示例

本文介绍了HashMap的基本概念,包括其特点、添加元素(put)、获取值(get)、大小与清空(size/clear)、键值判断(containsKey/containsValue)等核心方法。同时涵盖了Set集合操作,如entrySet/keySet/remove及值的替换和集合合并等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

** HashMap是一个存储键值对的集合,键和值都可以是任意数据类型。但要求键不能重复。如果出现两个相同的键,后面的会把前面的覆盖掉。**
下面是它的常用方法:
1、put(key, value); 往集合中添加元素的方法。

//这里键是String类型,值是Integer类型
Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);

2、get(key); 根据键来获取值。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Integer value = map.get("李四");	//获取值
System.out.println(value);	//会打印出20

3、size(); 返回集合的长度。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
int size = map.size();	//获取集合长度
System.out.println(size);	//会打印出3

4、clear(); 清空集合中的所有键值对,该方法没有返回值。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
map.clear();	//清空
System.out.println(map);	//会打印出{}

5、containsKey(key); 判断集合中是否包含指定键,返回布尔值,包含返回true,不包含返回false。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
boolean key = map.containsKey("张三");
System.out.println(key);	//会打印true
boolean key2 = map.containsKey("张三丰");
System.out.println(key2);	//会打印false

6、containsValue(value); 判断集合中是否包含指定值,返回布尔值,包含返回true,不包含返回false。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
boolean value = map.containsValue(20);
System.out.println(value);	//会打印true
boolean value2 = map.containsValue(50);
System.out.println(value2);	//会打印false

7、isEmpty(); 判断集合是否为空,返回布尔值,为空true,不为空false。

Map<String, Integer> map1 = new HashMap<>();
map1.put("赵六", 30);
boolean empty1 = map1.isEmpty();
System.out.println(empty1);	//会打印false
Map<String, Integer> map2 = new HashMap<>();
boolean empty2 = map2.isEmpty();
System.out.println(empty2);	//会打印true

8、equals(map); 判断两个集合是否相等,返回布尔值,相等true,不相等false。

Map<String, Integer> map1 = new HashMap<>();
map1.put("赵六", 30);
Map<String, Integer> map2 = new HashMap<>();
map2.put("赵六", 30);
boolean equals = map1.equals(map2);
System.out.println(equals);	//会打印true

9、entrySet(); 返回键和值的Set集合。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//遍历这个Set集合
for (Map.Entry<String, Integer> entry : entrySet) {
    System.out.println(entry);	
    /*	会依次打印出:
	李四=20
	张三=10
	王五=30
	*/
}

10、keySet(); 返回键的Set集合。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Set<String> keySet = map.keySet();
for (String s : keySet) {	//遍历这个Set集合
    System.out.println(s);
    /*	会依次打印出:
	李四
	张三
	王五
	*/
}

11、remove(); 有两种用法:

  • 第一种:remove(key); 根据指定的键来移除指定的元素,返回键对应的值。
  • 第二种:remove(key,value);根据指定的键和值来移除指定的元素,布尔值。
Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Integer remove = map.remove("张三");
System.out.println(remove);	//返回10
boolean remove1 = map.remove("王五", 40);
System.out.println(remove1);	//返回true

12、putAll(map); 将另一个map中的全部元素添加进来,如果key重复会被覆盖掉。

Map<String, Integer> map1 = new HashMap<>();
map1.put("赵六", 10);
Map<String, Integer> map2 = new HashMap<>();
map2.put("孙七", 50);
map1.putAll(map2);
System.out.println(map1);	
//会打印 {孙七=50, 赵六=10}

13、replace(); 有两种用法:

  • 第一种:replace(key,value); 替换指定键的值,返回替换前的值。
  • 第二种:replace(key,oldValue,newValue); 替换指定键值对的值,oldValue是旧的值,newValue是要替换的新的值,返回布尔值,替换成功返回true。
Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Integer replace = map.replace("王五", 80);
System.out.println(replace);	//打印旧的值 30
boolean replace1 = map.replace("张三", 10, 60);
System.out.println(replace1);	//打印true

14、replaceAll(); 将值按指定的规则进行替换。

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
map.replaceAll((key, value) -> value + 100);
System.out.println(map);
//{李四=120, 张三=110, 王五=130}

15、values(); 返回值的Collection集合;

Map<String, Integer> map = new HashMap<>();
map.put("张三", 10);
map.put("李四", 20);
map.put("王五", 30);
Collection<Integer> values = map.values();
for (Integer value : values) {
	/* 会打印
	20
	10
	30
	*/
    System.out.println(value);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值