Map与Set
?前言?
📘 博客主頁:to Keep博客主頁
🙆歡迎關(guān)注,👍點(diǎn)贊,📝留言評論
?首發(fā)時(shí)間:2022年3月4日
📨 博主碼云地址:博主碼云地址
📕參考書籍:java核心技術(shù) 卷1
📢編程練習(xí):??途W(wǎng)+力扣網(wǎng)
由于博主目前也是處于一個(gè)學(xué)習(xí)的狀態(tài),如有講的不對的地方,請一定聯(lián)系我予以改正!!!
文章目錄
- 1 Map與Set概念
- 1.1 搜索
- 1.2 模型
- 2 Map的使用(HashMap為例)
- 2.1 put()方法
- 2.2 remove()方法
- 2.3 get()方法
- 2.4 getOrDefault()方法
- 2.5 entrySet()方法
- 2.6 Map要點(diǎn)
- 3 Set的使用(HashSet為例)
- 3.1 add()方法
- 3.2 iterator()方法
- 3.3 toArray()方法
- 3.4 Set要點(diǎn)
- 4 HashMap與HashSet對比
1 Map與Set概念
1.1 搜索
Map與Set是一種專門用來進(jìn)行搜索的容器與數(shù)據(jù)結(jié)構(gòu),其搜索效率與其具體的實(shí)例化子類有關(guān)
以前常見的搜索方式:
以上述排序比較適合靜態(tài)類型的查找,即一般不會(huì)對區(qū)間進(jìn)行插入和刪除操作
而在實(shí)際運(yùn)用當(dāng)中可能需要?jiǎng)討B(tài)的查找,比如:
而我們的Map與Set就是一種適合動(dòng)態(tài)查找的容器集合
1.2 模型
一般把搜索的數(shù)據(jù)稱為關(guān)鍵字(Key),和關(guān)鍵字對應(yīng)的稱為值(Value),將其稱之為Key-value的鍵值對,所以模型會(huì)有兩種:
一般把搜索的數(shù)據(jù)稱為關(guān)鍵字(Key),和關(guān)鍵字對應(yīng)的稱為值(Value),將其稱之為Key-value的鍵值對,所以模型會(huì)有兩種:
1 純 key 模型,比如:
有一個(gè)英文詞典,快速查找一個(gè)單詞是否在詞典中
快速查找某個(gè)名字在不在通訊錄中
2 Key-Value 模型,比如:
1 統(tǒng)計(jì)文件中每個(gè)單詞出現(xiàn)的次數(shù),統(tǒng)計(jì)結(jié)果是每個(gè)單詞都有與其對應(yīng)的次數(shù):<單詞,單詞出現(xiàn)的次數(shù)>
2 梁山好漢的江湖綽號:每個(gè)好漢都有自己的江湖綽號
而Map中存儲(chǔ)的就是key-value的鍵值對,Set中只存儲(chǔ)了Key。
2 Map的使用(HashMap為例)
2.1 put()方法
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hello",2);map.put("hell",1);map.put("asda",5);map.put("hello",1);System.out.println(map);} }運(yùn)行結(jié)果:
{asda=5, hello=1, hell=1}
1 put方法是將鍵值對存放到HashMap中
2 對于重復(fù)放入的數(shù)據(jù),后面的數(shù)據(jù)會(huì)將前面的數(shù)據(jù)掩蓋掉
3 對于放入的數(shù)據(jù)前后是哈希表底層決定的,不是后放的就可以先出
2.2 remove()方法
刪除Key對應(yīng)的映射關(guān)系
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);map.remove("hell");System.out.println(map);} }運(yùn)行結(jié)果:
{asda=5}
2.3 get()方法
返回key對應(yīng)的value
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);int value = map.get("hell");System.out.println(value);} }運(yùn)行結(jié)果:
1
2.4 getOrDefault()方法
返回 key 對應(yīng)的 value,key 不存在,返回默認(rèn)值,默認(rèn)值是可以我們自己設(shè)置的
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);int value1 = map.getOrDefault("hell",0);int value2 = map.getOrDefault("hello",1);System.out.println(value1);System.out.println(value2);} }運(yùn)行結(jié)果:
1
1
2.5 entrySet()方法
函數(shù)返回值類型為:Set<Map.Entry<K, V>>,返回所有的 key-value 映射關(guān)系
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);Set<Map.Entry<String,Integer>> set = map.entrySet();System.out.println(set);} }運(yùn)行結(jié)果:
[asda=5, hell=1]
Map.Entry<K,V>獲取Key的值
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);Set<Map.Entry<String,Integer>> set = map.entrySet();for (Map.Entry<String,Integer> tmp:set) {System.out.print(tmp.getKey()+" ");}} }運(yùn)行結(jié)果:
asda hell
Map.Entry<K,V>獲取value的值
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);Set<Map.Entry<String,Integer>> set = map.entrySet();for (Map.Entry<String,Integer> tmp:set) {System.out.print(tmp.getValue()+" ");}} }運(yùn)行結(jié)果:
5 1
Map.Entry<K,V>更改value的值
public class test1 {public static void main(String[] args) {Map<String,Integer> map = new HashMap<>();map.put("hell",1);map.put("asda",5);Set<Map.Entry<String,Integer>> set = map.entrySet();for (Map.Entry<String,Integer> tmp:set) {tmp.setValue(54);System.out.print(tmp.getValue()+" ");}} }運(yùn)行結(jié)果:
54 54
注意: Map.Entry<K,V>并沒有提供設(shè)置Key的方法
2.6 Map要點(diǎn)
Map官方文檔
3 Set的使用(HashSet為例)
3.1 add()方法
添加元素,但是重復(fù)的元素是不可以添加成功的!
public class test1 {public static void main(String[] args) {Set<String> set = new HashSet<>();set.add("hello");set.add("hello");set.add("well");System.out.println(set);} }運(yùn)行結(jié)果:
[well, hello]
3.2 iterator()方法
public class test1 {public static void main(String[] args) {Set<String> set = new HashSet<>();set.add("hello");set.add("hello");set.add("well");Iterator<String> it = set.iterator();//返回迭代器while (it.hasNext()){//遍歷迭代器內(nèi)容String str = it.next();System.out.println(str);}} }運(yùn)行結(jié)果:
well
hello
3.3 toArray()方法
將set中的元素轉(zhuǎn)換為數(shù)組返回
public class test1 {public static void main(String[] args) {Set<String> set = new HashSet<>();set.add("hello");set.add("hello");set.add("well");Object[] str = set.toArray();//需要Object接收for (Object e:str) {System.out.println(e);}} }運(yùn)行結(jié)果:
well
hello
3.4 Set要點(diǎn)
Set官方文檔
4 HashMap與HashSet對比
之后我們將利用Map與Set所學(xué),進(jìn)行有關(guān)面試題的練習(xí)!!!
總結(jié)
- 上一篇: 源码必备知识:泛型
- 下一篇: Map与Set的经典OJ题