日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Map与Set

發布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Map与Set 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?前言?

📘 博客主頁:to Keep博客主頁
🙆歡迎關注,👍點贊,📝留言評論
?首發時間:2022年3月4日
📨 博主碼云地址:博主碼云地址
📕參考書籍:java核心技術 卷1
📢編程練習:牛客網+力扣網
由于博主目前也是處于一個學習的狀態,如有講的不對的地方,請一定聯系我予以改正!!!

文章目錄

  • 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要點
  • 3 Set的使用(HashSet為例)
    • 3.1 add()方法
    • 3.2 iterator()方法
    • 3.3 toArray()方法
    • 3.4 Set要點
  • 4 HashMap與HashSet對比

1 Map與Set概念

1.1 搜索

Map與Set是一種專門用來進行搜索的容器與數據結構,其搜索效率與其具體的實例化子類有關

以前常見的搜索方式:

  • 直接遍歷,時間復雜度為O(N),元素如果比較多效率會非常慢
  • 二分查找,時間復雜度為 ,但搜索前必須要求序列是有序
  • 以上述排序比較適合靜態類型的查找,即一般不會對區間進行插入和刪除操作
    而在實際運用當中可能需要動態的查找,比如:

  • 根據姓名查詢考試成績
  • 通訊錄,即根據姓名查詢聯系方式
  • 不重復集合,即需要先搜索關鍵字是否已經在集合中
  • 而我們的Map與Set就是一種適合動態查找的容器集合

    1.2 模型

    一般把搜索的數據稱為關鍵字(Key),和關鍵字對應的稱為值(Value),將其稱之為Key-value的鍵值對,所以模型會有兩種:
    一般把搜索的數據稱為關鍵字(Key),和關鍵字對應的稱為值(Value),將其稱之為Key-value的鍵值對,所以模型會有兩種:
    1 純 key 模型,比如:

    有一個英文詞典,快速查找一個單詞是否在詞典中
    快速查找某個名字在不在通訊錄中

    2 Key-Value 模型,比如:

    1 統計文件中每個單詞出現的次數,統計結果是每個單詞都有與其對應的次數:<單詞,單詞出現的次數>
    2 梁山好漢的江湖綽號:每個好漢都有自己的江湖綽號

    而Map中存儲的就是key-value的鍵值對,Set中只存儲了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);} }

    運行結果:

    {asda=5, hello=1, hell=1}

    1 put方法是將鍵值對存放到HashMap中
    2 對于重復放入的數據,后面的數據會將前面的數據掩蓋掉
    3 對于放入的數據前后是哈希表底層決定的,不是后放的就可以先出

    2.2 remove()方法

    刪除Key對應的映射關系

    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);} }

    運行結果:

    {asda=5}

    2.3 get()方法

    返回key對應的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);} }

    運行結果:

    1

    2.4 getOrDefault()方法

    返回 key 對應的 value,key 不存在,返回默認值,默認值是可以我們自己設置的

    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);} }

    運行結果:

    1
    1

    2.5 entrySet()方法

    函數返回值類型為:Set<Map.Entry<K, V>>,返回所有的 key-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();System.out.println(set);} }

    運行結果:

    [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()+" ");}} }

    運行結果:

    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()+" ");}} }

    運行結果:

    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()+" ");}} }

    運行結果:

    54 54

    注意: Map.Entry<K,V>并沒有提供設置Key的方法

    2.6 Map要點

    Map官方文檔

  • Map是一個接口,不能直接實例化對象,如果要實例化對象只能實例化其實現類TreeMap或者HashMap
  • Map中存放鍵值對的Key是唯一的,value是可以重復的
  • 在Map中插入鍵值對時,TreeMap中key不能為空,否則就會拋NullPointerException異常,但是value可以為空,而HashMap中的Key是可以為空的
  • Map中的Key可以全部分離出來,存儲到Set中來進行訪問(因為Key不能重復)。
  • Map中的value可以全部分離出來,存儲在Collection的任何一個子集合中(value可能有重復)。
  • Map中鍵值對的Key不能直接修改,value可以修改,如果要修改key,只能先將該key刪除掉,然后再來進行 重新插入。
  • 3 Set的使用(HashSet為例)

    3.1 add()方法

    添加元素,但是重復的元素是不可以添加成功的!

    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);} }

    運行結果:

    [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()){//遍歷迭代器內容String str = it.next();System.out.println(str);}} }

    運行結果:

    well
    hello

    3.3 toArray()方法

    將set中的元素轉換為數組返回

    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);}} }

    運行結果:

    well
    hello

    3.4 Set要點

    Set官方文檔

  • Set是繼承自Collection的一個接口類
  • Set中只存儲了key,并且要求key一定要唯一
  • Set的底層是使用Map來實現的,其使用key與Object的一個默認對象作為鍵值對插入到Map中的
  • Set最大的功能就是對集合中的元素進行去重
  • 實現Set接口的常用類有TreeSet和HashSet,還有一個LinkedHashSet,LinkedHashSet是在HashSet的基礎上維護了一個雙向鏈表來記錄元素的插入次序。
  • Set中的Key不能修改,如果要修改,先將原來的刪除掉,然后再重新添加
  • Set中不能插入null的key。
  • 4 HashMap與HashSet對比


    之后我們將利用Map與Set所學,進行有關面試題的練習!!!

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的Map与Set的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。