天天看點

Map集合概述和特點的知識點總結(純幹貨)

A:Map接口概述

* 一個映射不能包含重複的鍵

* 每個鍵最多隻能映射到一個值

  • B:Map接口和Collection接口的不同
    • Map是雙列的,Collection是單列的
    • Map的鍵唯一,Collection的子體系Set是唯一的
    • Map集合的資料結構值針對鍵有效,跟值無關;Collection集合的資料結構是針對元素有效

###集合架構(Map集合的功能概述)

  • A:Map集合的功能概述
    • a:添加功能
      • V put(K key,V value):添加元素。
        • 如果鍵是第一次存儲,就直接存儲元素,傳回null
        • 如果鍵不是第一次存在,就用值把以前的值替換掉,傳回以前的值
    • b:删除功能
      • void clear():移除所有的鍵值對元素
      • V remove(Object key):根據鍵删除鍵值對元素,并把值傳回
    • c:判斷功能
      • boolean containsKey(Object key):判斷集合是否包含指定的鍵
      • boolean containsValue(Object value):判斷集合是否包含指定的值
      • boolean isEmpty():判斷集合是否為空
    • d:擷取功能
      • Set<Map.Entry<K,V>> entrySet():
      • V get(Object key):根據鍵擷取值
      • Set keySet():擷取集合中所有鍵的集合
      • Collection values():擷取集合中所有值的集合
    • e:長度功能
      • int size():傳回集合中的鍵值對的個數

###集合架構(Map集合的周遊之鍵找值)

  • A:鍵找值思路:(底層是雜湊演算法)
    • 擷取所有鍵的集合
    • 周遊鍵的集合,擷取到每一個鍵
    • 根據鍵找值
  • B:案例示範
    • Map集合的周遊之鍵找值
      HashMap<String, Integer> hm = new HashMap<>();
        hm.put("唐僧", 23);
        hm.put("孫悟空", 24);
        hm.put("豬八戒", 25);
        hm.put("沙僧", 26);
        
        /*Set<String> keySet = hm.keySet();			//擷取集合中所有的鍵
        Iterator<String> it = keySet.iterator();	//擷取疊代器
        while(it.hasNext()) {						//判斷單列集合中是否有元素
        	String key = it.next();					//擷取集合中的每一個元素,其實就是雙列集合中的鍵
        	Integer value = hm.get(key);			//根據鍵擷取值
        	System.out.println(key + "=" + value);	//列印鍵值對
        }*/
        
        for(String key : hm.keySet()) {				//增強for循環疊代雙列集合第一種方式
        	System.out.println(key + "=" + hm.get(key));
        }
                 

###集合架構(Map集合的周遊之鍵值對對象找鍵和值)

  • A:鍵值對對象找鍵和值思路:
    • 擷取所有鍵值對對象的集合
    • 周遊鍵值對對象的集合,擷取到每一個鍵值對對象
    • 根據鍵值對對象找鍵和值
  • B:案例示範
    • Map集合的周遊之鍵值對對象找鍵和值
      HashMap<String, Integer> hm = new HashMap<>();
        hm.put("唐僧", 23);
        hm.put("孫悟空", 24);
        hm.put("豬八戒", 25);
        hm.put("沙僧", 26);
        /*Set<Map.Entry<String, Integer>> entrySet = hm.entrySet();	//擷取所有的鍵值對象的集合
        Iterator<Entry<String, Integer>> it = entrySet.iterator();//擷取疊代器
        while(it.hasNext()) {
        	Entry<String, Integer> en = it.next();				//擷取鍵值對對象
        	String key = en.getKey();								//根據鍵值對對象擷取鍵
        	Integer value = en.getValue();							//根據鍵值對對象擷取值
        	System.out.println(key + "=" + value);
        }*/
        
        for(Entry<String,Integer> en : hm.entrySet()) {
        	System.out.println(en.getKey() + "=" + en.getValue());
        }
                 

###集合架構(LinkedHashMap的概述和使用)

* LinkedHashMap的特點

* 底層是連結清單實作的可以保證怎麼存就怎麼取

###集合架構(統計字元串中每個字元出現的次數)

  • A:案例示範
    • 需求:統計字元串中每個字元出現的次數
    • String str = "aaaabbbcccccccccc";
        char[] arr = str.toCharArray();						//将字元串轉換成字元數組
        HashMap<Character, Integer> hm = new HashMap<>();	//建立雙列集合存儲鍵和值
        
        for(char c : arr) {									//周遊字元數組
        	/*if(!hm.containsKey(c)) {						//如果不包含這個鍵
        		hm.put(c, 1);								//就将鍵和值為1添加
        	}else {											//如果包含這個鍵
        		hm.put(c, hm.get(c) + 1);					//就将鍵和值再加1添加進來
        	}
        	
        	//hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
        	Integer i = !hm.containsKey(c) ? hm.put(c, 1) : hm.put(c, hm.get(c) + 1);
        			}
        
        for (Character key : hm.keySet()) {					//周遊雙列集合
        	System.out.println(key + "=" + hm.get(key));
        }
                 

###集合架構(HashMap和Hashtable的差別)

  • A:面試題
    • HashMap和Hashtable的差別
      • Hashtable是JDK1.0版本出現的,是線程安全的,效率低,HashMap是JDK1.2版本出現的,是線程不安全的,效率高
      • Hashtable不可以存儲null鍵和null值,HashMap可以存儲null鍵和null值

###集合架構(Collections工具類的概述和常見方法講解)

  • A:Collections類概述
    • 針對集合操作 的工具類
  • B:Collections成員方法
  • public static <T> void sort(List<T> list)
      public static <T> int binarySearch(List<?> list,T key)
      public static <T> T max(Collection<?> coll)
      public static void reverse(List<?> list)
      public static void shuffle(List<?> list)