目錄
1.HashMap
1.1Map的常用方法
1.2HashMap的使用案例
1.HashMap
基于哈希表的實現(xiàn)的Map接口。
Map底層結(jié)構(gòu) | HashMap |
底層結(jié)構(gòu) | 哈希桶 |
插入/刪除/查找時間復(fù)雜度 | O(1) |
是否有序 | 無序 |
線程安全 | 不安全 |
插入/刪除/查找區(qū)別 | 通過哈希函數(shù)計算哈希地址 |
比較與覆寫 | 自定義類型需要覆寫equals和 hashCode方法 |
1.1Map的常用方法
方法 | 解釋 |
V get(Object key) | 返回 key 對應(yīng)的 value |
V getOrDefault(Object key, V defaultValue) | 返回 key 對應(yīng)的 value,key 不存在,返回默認值 |
V put(K key, V value) | 設(shè)置 key 對應(yīng)的 value |
V remove(Object key) | 刪除 key 對應(yīng)的映射關(guān)系 |
Set<K> keySet() | 返回所有 key 的不重復(fù)集合 |
Collection<V> values() | 返回所有 value 的可重復(fù)集合 |
Set<Map.Entry<K, V>> entrySet() | 返回所有的 key-value 映射關(guān)系 |
boolean containsKey(Object key) | 判斷是否包含 key |
boolean containsValue(Object value) | 判斷是否包含 value |
注意:Map.Entry<>是Map內(nèi)部實現(xiàn)的用來存放key-value相應(yīng)鍵值對的內(nèi)部類?
? ? ? ? 其內(nèi)部有g(shù)etKey(),getValue與setValue()方法
1.2HashMap的使用案例
創(chuàng)建一個HashMap,及put的使用
import java.util.*;
public class Test {
public static void Map(){
Map<String,Integer> map = new HashMap<>();
//創(chuàng)建一個HashMap key的類型為"String" value的類型為"Integer"
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",100);
map.put(null,null);
map.put("d",4);//當(dāng)key存在時,則會更新value
//向map中put入五個鍵值對
//注意噢,在HashMap中的key和value都可以為null
int size = map.size();
System.out.println("size = " + size);
//通過size()方法,得到map中鍵值對的數(shù)量.此時size == 5
int retGet = map.get("a");
System.out.println("retGet = " + retGet);
//通過get()方法,得到對應(yīng)key的value值.此時retGet為1
//retGet = map.get("z");//map中的key沒有"z",此條程序會報錯
//注意!!! 當(dāng)使用get方法時,map中沒有對應(yīng)的key值時,程序會報錯
int retGOD = map.getOrDefault("z",-1);
System.out.println("retGOD = " + retGOD);
//此處retGOD為-1
//通過getOrDefault()方法,得到對應(yīng)的key的value值,如果key值不存在
//則返回我們設(shè)置的默認值"-1".
//與get()不同的是,查找的key如果不存在于map中,也不會進行報錯
boolean retConK = map.containsKey("a");
boolean retConV = map.containsValue(4);
System.out.println("retConk = " + retConK);
System.out.println("retConV = " + retConV);
//通過containsKey()方法查找map中是否存在對應(yīng)的key值
//containsValue()方法查找map中是否存在對應(yīng)的value,可以是一個或多個
Set<String> set = map.keySet();
for (String s:set) {
System.out.print(s + " ");
}
System.out.println();
//通過keySet()方法,返回map中所有的key值,并以set的形式返回
int retRemove = map.remove("a");
System.out.println("retRemover = " + retRemove);
//通過remove()方法,根據(jù)key值刪除相應(yīng)的鍵值對,并返回刪除的value值
int retReplace = map.replace("b",10);
System.out.println("retReplace = " + retReplace);
//通過replace()方法,更新key的value--好像直接put也一樣?
Collection<Integer> collection = map.values();
// for (int i:collection) { 此方法因為value中有一個null,直接遍歷會有一個空指針的報錯,int類型也不能與null比較
// System.out.println(i + " "); 于是乎,有了以下使用迭代器的方式來遍歷
// }
// System.out.println();
Iterator<Integer> iterator = collection.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
//通過keySet()方法,返回map中所有的value值,以collection的形式返回
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> s : map.entrySet()) {
System.out.print(s.getKey() + "->" + s.getValue() + " ");
}
System.out.println();
//通過entrySet()方法,得到key與value的對應(yīng)關(guān)系,并使用其getKey與getValue得到相應(yīng)的值
}
public static void main(String[] args) {
Map();
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-527033.html文章來源:http://www.zghlxwxcb.cn/news/detail-527033.html
?
到了這里,關(guān)于[JAVA數(shù)據(jù)結(jié)構(gòu)]HashMap的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!