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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【Java】如何理解Java中的双列集合Map?

發布時間:2024/7/5 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java】如何理解Java中的双列集合Map? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 Map<K,V>接口

1.1 特點

  • 雙列集合一個元素包含倆值
  • Key不可以重復,Value可以重復
  • Key和Value一一對應
  • Key和Value可以時任意類型
  • 1.2 常用方法

    public V put(K key, V value): 不重復返回null,重復返回value

    public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);Integer out1 = persons.put("李四", 20);Integer out2 = persons.put("李四", 30);System.out.println(out1);//不重復返回nullSystem.out.println(out2);//重復返回原來的value=20System.out.println(persons);//{李四=30, 張三=19}}

    public V remove(Object Key):指定鍵,刪除鍵值對,返回刪除鍵對應的值,鍵不存在則返回null

    public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);persons.put("李四", 20);Integer out1 = persons.remove("張三");System.out.println(persons);//{李四=20}System.out.println(out1);//19Integer out2 = persons.remove("王五");System.out.println(out2);//null}

    public V get(Object Key):通過鍵來獲取值

    public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);System.out.println(persons.get("張三"));//19System.out.println(persons.get("李四"));//null}

    public boolean containsKey(Object Key):鍵是否存在

    public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);System.out.println(persons.containsKey("張三"));//trueSystem.out.println(persons.containsKey("李四"));//false}

    1.3 遍歷Map集合

    方法1:遍歷鍵找值法

    public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();map.put("張三",19);map.put("李四",20);map.put("王五",21);//1. 取出Map的所有key存在Set中Set<String> set = map.keySet();//2.1 while遍歷set,獲取key,map.get獲取valueIterator<String> it = set.iterator();while(it.hasNext()){String key = it.next();Integer value = map.get(key);System.out.println(key + " " + value);}//2.2 foreach法遍歷for(String key: set){System.out.println(key + " " + map.get(key));}}

    方法2: Entry鍵值對對象
    Map接口中有一個內部接口Entry
    作用:Map一創建,就在Map中創建一個Entry對象,用來記錄鍵和值,得到二者的映射關系。(相當于結婚證)

  • Set<Map.Entry<K, V>> entrySet() 把Map集合的多個Entry對象取出存在Set集合中。
  • 遍歷Set集合,獲取每個Entry對象。
  • getKey()獲取Key,getValue()獲取Value。
  • public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();map.put("張三",19);map.put("李四",20);map.put("王五",21);//1. 取出Map的所有entry對象存在Set中Set<Map.Entry<String, Integer>>set = map.entrySet();//2.1 while遍歷set,獲取entry,再分別getKey和ValueIterator<Map.Entry<String, Integer>> it = set.iterator();while(it.hasNext()){Map.Entry<String, Integer> entry = it.next();String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " " + value);}//2.2 foreach法遍歷for(Map.Entry<String, Integer> entry: set){System.out.println(entry.getKey() + " " + entry.getValue());}}

    2 Map接口的實現類—— HashMap

    2.1 特點

  • 底層是哈希表(JDK1.8后:數組+鏈表/紅黑樹) 查詢快
  • 無序集合,存取無序
  • 2.2 存儲自定義類型鍵值

    注意:因為Map集合要保證Key是唯一的,所以作為Key的元素必須重寫hashCode方法equals方法,從而保證Key的唯一性。

    public static void main(String[] args) {Map<Person, Integer> map1 = new HashMap<Person, Integer>();map1.put(new Person("李四",10),170);map1.put(new Person("李四",10),180);System.out.println(map1);//重寫前輸出:{Person{name='李四', age=10}=180, Person{name='李四', age=10}=170}//重寫后輸出:{Person{name='李四', age=10}=180}}

    3 HashMap的子類——LinkedHashMap

    3.1 特點

  • 底層是哈希表+鏈表 保證迭代順序
  • 有序集合,存取有序
  • 3.2 使用示例

    public static void main(String[] args) {HashMap<String, Integer> map1 = new HashMap<String, Integer>();map1.put("a",1);map1.put("c",3);map1.put("b",2);System.out.println(map1);//存和取順序不同 {a=1, b=2, c=3}LinkedHashMap<String, Integer> map2 = new LinkedHashMap<>();map2.put("a",1);map2.put("c",3);map2.put("b",2);System.out.println(map2);//存和取順序相同{a=1, c=3, b=2}}

    4 Map接口的實現類——Hashtable

    4.1 Hashtable的特點

  • 鍵和值都不可以為空。
  • 最早的雙列集合,JDK1.0就有。
  • 它是單線程的,同步的,線程安全,速度慢。
  • 底層是一個哈希表。
  • 和vector(ArrayList)一樣再JDK1.2后被HashMap取代了。
  • Hashtable的子類Properties集合是唯一一個和IO流結合的集合。【仍活躍】
  • 4.2 示例

    public static void main(String[] args) {Hashtable<String,String> ht = new Hashtable<>();HashMap<String, String> hm = new HashMap<>();hm.put(null,null);hm.put("a",null);hm.put(null,"b");System.out.println(hm);//{null=b, a=null} // ht.put(null,null); // 報錯:java.lang.NullPointerException}

    5 Map集合練習

    5.1 統計字符出現次數

    遍歷字符串:charAt加索引遍歷 / toCharArray轉為數組
    for(int i = 0; i < str.length(); i++) —> str.charAt(i)
    for(char c: str.toCharArray()) —> c

    public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = sc.next();Map<Character, Integer> map = new HashMap<>();char[] strArray = str.toCharArray();for (int i = 0; i < strArray.length; i++) {if(!map.containsKey(strArray[i])){map.put(strArray[i],1);}else{int value = map.get(strArray[i]);map.put(strArray[i],++value);}}System.out.println(map);}

    5.2 斗地主升級版-玩家牌有序

    public static void main(String[] args) {//1. 準備牌面String[] colors = {"?","?","?","?"};String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};HashMap<Integer, String> pocker = new HashMap<>();pocker.put(0,"BK");pocker.put(1,"SK");int count = 2;for(String num: nums){for(String color: colors){pocker.put(count++,color+num);}}System.out.println(pocker);//2. 洗牌:打亂編號Set<Integer> set = pocker.keySet();ArrayList<Integer> list = new ArrayList<>();list.addAll(set);Collections.shuffle(list);ArrayList<Integer> left = new ArrayList<>();ArrayList<Integer> p1 = new ArrayList<>();ArrayList<Integer> p2 = new ArrayList<>();ArrayList<Integer> p3 = new ArrayList<>();for (int i = 0; i < pocker.size(); i++) {Integer p = list.get(i);if(i>=51){left.add(p);}else{if(i%3 == 0){p1.add(p);}else if(i%3 == 1){p2.add(p);}else{p3.add(p);}}}//3. 發牌:對編號排序 取出標號對應的值Collections.sort(p1);Collections.sort(p2);Collections.sort(p3);Collections.sort(left);ArrayList<String> player1 = new ArrayList<String>();ArrayList<String> player2 = new ArrayList<String>();ArrayList<String> player3 = new ArrayList<String>();ArrayList<String> dipai = new ArrayList<String>();for(Integer i : p1){player1.add(pocker.get(i));}for(Integer i : p2){player2.add(pocker.get(i));}for(Integer i : p3){player3.add(pocker.get(i));}for(Integer i : left){dipai.add(pocker.get(i));}//4. 看牌System.out.println(player1);System.out.println(player2);System.out.println(player3);System.out.println(dipai);}

    6 JDK9對集合添加的優化

    Java9中添加了幾種集合工廠方法,更方便創建少量元素的集合、map實例。新的ListSetMap的靜態工廠方法可以更方便地創建集合的不可變實例。
    使用前提:集合中存儲的元素個數已經確定,不再改變時使用
    注意

  • 只適用于ListSetMap三個接口
  • 它是不能改變的集合,不可再用add和put
  • 不可以在調用時含有相同元素
  • 總結

    以上是生活随笔為你收集整理的【Java】如何理解Java中的双列集合Map?的全部內容,希望文章能夠幫你解決所遇到的問題。

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