【Java】HashMap 常用方法总结


HashMap 简单知识点

  • Map 集合即 Key-Value 的集合,前面加个 Hash,即散列,无序的。所以 HashMap 是一个用于存储Key-Value键值对的无序集合,每一个键值对也叫做Entry。
  • 在 JDK1.8 之前,HashMap 采用数组+链表实现,即使用链表处理冲突,同一 hash 值的节点都存储在一个链表里。但是当位于一个桶中的元素较多,即 hash 值相等的元素较多时,通过 key 值查找要遍历链表,时间复杂度为 O(N),效率较低。
  • 因此 JDK1.8 中,HashMap 采用数组+链表+红黑树实现,当链表长度超过阈值(8)时,将链表转换为红黑树,时间复杂度为 O(logN),这样大大减少了查找时间。

用一段代码来介绍常用方法:

package a;
import java.util.HashMap;

import java.util.Map.Entry;
public class Main{
    public static void main(String[] args){
    	HashMap<String,Integer> mp = new HashMap<String,Integer>();
    	mp.put("one",1);	//存放键值对
    	System.out.println(mp.get("one"));	//通过键取值,输出 1
    	System.out.println(mp.get("1"));	//通过键取值,不存在,输出 null
    	System.out.println("====================");
    	
    	System.out.println(mp.containsKey("one"));	//HashMap中是否包含该键,输出true
    	System.out.println(mp.containsKey("two"));	//不包含该键,输出false
    	System.out.println("====================");
    	
    	System.out.println(mp.containsValue(1));	//HashMap中是否包含该值,输出true
    	System.out.println(mp.containsValue(2));	//不包含该值,输出false
    	System.out.println("====================");
    	
    	System.out.println(mp.isEmpty());	//判断是否为空,输出false
    	System.out.println(mp.size()); 		//输出 HasMap 的长度,1
    	System.out.println("====================");
    	
    	mp.remove("one");	//从HashMap中删除该键,值也会被删除
    	System.out.println(mp.get("one"));	//输出null
    	System.out.println(mp.containsKey("one"));	//输出false
    	System.out.println(mp.containsValue(1));	//输出false
    	//也可以通过 mp.remove("one",1); 把键和值一起删掉
    	System.out.println("====================");
    	
    	mp.put("one", 1);
    	mp.put("two", 2);
    	mp.put("three", 3);
    	System.out.println(mp.values());//输出所有值,[1, 2, 3]
    	System.out.println(mp.keySet());//输出所有键,[one, two, three]
    	System.out.println(mp.entrySet());//输出所有键和值,[one=1, two=2, three=3],中括号
    	System.out.println("====================");
    	
    	HashMap<String,Integer> mp2 = new HashMap<String,Integer>();
    	mp2.put("four", 4);
    	mp.putAll(mp2);	//添加同类型另一个HashMap,放进头部
    	System.out.println(mp);	//输出整个HashMap的键和值,{four=4, one=1, two=2, three=3},大括号
    	System.out.println("====================");
    	
    	mp.replace("one", 5);	//替换键的值,java8才有
    	mp.replace("two", 2 , 6);	//替换键的旧值为新值
    	System.out.println(mp);	//输出{four=4, one=5, two=6, three=3}
    	System.out.println("====================");
    	
    	Object mp3 = mp.clone();	//克隆一个,顺序随机
    	System.out.println(mp3);	//输出{two=6, three=3, four=4, one=5}
    	System.out.println("====================");
    	
    	for(String key:mp.keySet())	//遍历整个HashMap的键
    		System.out.print(key+' ');//输出four one two three 
    	System.out.println();
    	for(Integer values:mp.values())	//遍历整个HashMap的值
    		System.out.print(values+' ');//输出36373835,并不是4 5 6 3 ,说明该方法不能输出值
    	System.out.println();
    	for(Entry<String,Integer> entry:mp.entrySet()) {	//遍历整个HashMap,输出键值
    		String key = entry.getKey();
    		Integer value = entry.getValue();
    		System.out.print(key+'='+value+' ');	//输出four=4 one=5 two=6 three=3 
    	}
    	System.out.println();
    	System.out.println("====================");
    	
    	mp.clear();	//清空数组
    	System.out.println(mp); 	//输出{}
    	System.out.println("====================");
    }
}

输出结果:

1
null
====================
true
false
====================
true
false
====================
false
1
====================
null
false
false
====================
[1, 2, 3]
[one, two, three]
[one=1, two=2, three=3]
====================
{four=4, one=1, two=2, three=3}
====================
{four=4, one=5, two=6, three=3}
====================
{two=6, three=3, four=4, one=5}
====================
four one two three 
36373835
four=4 one=5 two=6 three=3 
====================
{}
====================
发布了123 篇原创文章 · 获赞 57 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44668898/article/details/104060609