Java——HashMap的一些常用方法

1.put方法

put用于向表中添加键值对

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.print(hashmap);		
	}

输出结果为

{1=张三, 2=李四}

2.get方法

get方法用于获取key对应的value

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println(hashmap.get(1));
		System.out.println(hashmap.get(2));		
	}

输出结果为

张三
李四

3.remove方法

remove方法用于删除key对应的键值对

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		hashmap.remove(1);
		System.out.println(hashmap.get(1));
		System.out.println(hashmap.get(2));		
	}

运行结果

null
李四

4.clear方法

clear方法会删除所有键值对

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		hashmap.clear();
		System.out.println(hashmap.get(1));
		System.out.println(hashmap.get(2));		
	}

运行结果

null
null

5.size方法

size方法用于计算hashmap的大小

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println(hashmap.size());	
	}

运行结果

2

6.isEmpty方法

isEmpty方法判断hashmap是否为空,为空返回true,否则返回false

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		hashmap.clear();
		System.out.println(hashmap.isEmpty());	
	}

运行结果

true

7. replace方法

replace方法替换 hashMap 中是指定的 key 对应的 value

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println("替换前:"+hashmap.get(1));	
		hashmap.replace(1,"老王");
		System.out.println("替换后:"+hashmap.get(1));			
	}

运行结果

替换前:张三
替换后:老王

8.containsKey方法

containsKey方法用于如果此映射包含指定键的映射,则返回 true 。

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println(hashmap.containsKey(3));	
		hashmap.put(3, "老王");
		System.out.println(hashmap.containsKey(3));			
	}

运行结果

false
true

9.containsValue方法

containsValue方法如果此映射将一个或多个键映射到指定值,则返回 true 。

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println(hashmap.containsValue("张三"));	
		hashmap.remove(1);
		System.out.println(hashmap.containsValue("张三"));		
	}
true
false

10.getOrDefault方法

getOrDefault() 方法获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值。

	public  static void main(String[] args) {
		HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
		hashmap.put(1, "张三");
		hashmap.put(2, "李四");
		System.out.println(hashmap.getOrDefault(1, "老王"));			
		System.out.println(hashmap.getOrDefault(3, "老王"));		
	}

运行结果

张三
老王
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中的HashMap是一种常用的集合类,用于存储键值对。它继承自AbstractMap类,并且实现了Map接口。与HashTable相比,HashMap具有更好的性能和扩展性。[1] HashMap的底层原理是使用哈希表来实现的,它通过将键映射到存储桶中来存储和获取值。HashMap使用键的hashCode()方法来计算哈希码,然后根据哈希码将键值对存储在相应的存储桶中。在存储和获取值时,HashMap会根据键的哈希码进行快速查找,从而实现了O(1)的平均时间复杂度。 在HashMap中,键和值都允许为null,但是只能有一个为null的键。如果多个键映射到同一个存储桶,HashMap会使用链表或红黑树来解决冲突。当链表长度超过一定阈值时,链表会被转换为红黑树,以提高查找效率。 除了HashMap,还有其他一些相关的类可以用来实现线程安全的HashMap,例如Collections.synchronizedMap()方法可以将HashMap转换为同步的Map。这样可以在多线程环境中安全地使用HashMap。不过,由于性能的原因,这种方式不常用。 总结来说,JavaHashMap是一种常用的集合类,使用哈希表来存储键值对。它具有良好的性能和扩展性,允许键和值为null。在多线程环境中可以使用同步的Map来实现线程安全。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java集合 —— HashMap原理解析](https://blog.csdn.net/m0_56602092/article/details/130338081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [【javaHashMap底层实现原理及面试题](https://blog.csdn.net/twotwo22222/article/details/128426417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天地神仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值