Java基礎——Java重點基礎之集合框架(四)


一,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集合的遍歷之鍵值對對象找鍵和值

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


五,HashMap集合鍵是Student值是String的案例

/*
	 * * A:案例演示
	 * HashMap集合鍵是Student值是String的案例
	 * 鍵是學生對象,代表每一個學生
	 * 值是字符串對象,代表學生歸屬地
	 */
	public static void main(String[] args) {
		HashMap<Student, String> hm = new HashMap<>();
		hm.put(new Student("張三", 23), "北京");
		hm.put(new Student("張三", 23), "上海");
		hm.put(new Student("李四", 24), "廣州");
		hm.put(new Student("王五", 25), "深圳");
		
		System.out.println(hm);
	}

六,LinkedHashMap的概述和使用

  • LinkedHashMap的特點
  • 底層是鏈表實現的可以保證怎麼存就怎麼取

七,TreeMap集合鍵是Student值是String的案例

TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {

			@Override
			public int compare(Student s1, Student s2) {
				int num = s1.getName().compareTo(s2.getName());		//按照姓名比較
				return num == 0 ? s1.getAge() - s2.getAge() : num;
			}
		});
		tm.put(new Student("張三", 23), "北京");
		tm.put(new Student("李四", 13), "上海");
		tm.put(new Student("趙六", 43), "深圳");
		tm.put(new Student("王五", 33), "廣州");
		
		System.out.println(tm);


八,統計字符串中每個字符出現的次數

                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嵌套HashMap

/**
	 * * A:案例演示
	 * 集合嵌套之HashMap嵌套HashMap
	 * 
	 * 需求:
	 * 雙元課堂有很多基礎班
	 * 第88期基礎班定義成一個雙列集合,鍵是學生對象,值是學生的歸屬地
	 * 第99期基礎班定義成一個雙列集合,鍵是學生對象,值是學生的歸屬地
	 * 
	 * 無論88期還是99期都是班級對象,所以爲了便於統一管理,把這些班級對象添加到雙元課堂集合中
	 */
	public static void main(String[] args) {
		//定義88期基礎班
		HashMap<Student, String> hm88 = new HashMap<>();
		hm88.put(new Student("張三", 23), "北京");
		hm88.put(new Student("李四", 24), "北京");
		hm88.put(new Student("王五", 25), "上海");
		hm88.put(new Student("趙六", 26), "廣州");
		
		//定義99期基礎班
		HashMap<Student, String> hm99 = new HashMap<>();
		hm99.put(new Student("唐僧", 1023), "北京");
		hm99.put(new Student("孫悟空",1024), "北京");
		hm99.put(new Student("豬八戒",1025), "上海");
		hm99.put(new Student("沙和尚",1026), "廣州");
		
		//定義雙元課堂
		HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
		hm.put(hm88, "第88期基礎班");
		hm.put(hm99, "第99期基礎班");
		
		//遍歷雙列集合
		for(HashMap<Student, String> h : hm.keySet()) {		//hm.keySet()代表的是雙列集合中鍵的集合
			String value = hm.get(h);						//get(h)根據鍵對象獲取值對象
			//遍歷鍵的雙列集合對象
			for(Student key : h.keySet()) {					//h.keySet()獲取集合總所有的學生鍵對象
				String value2 = h.get(key);
				
				System.out.println(key + "=" + value2 + "=" + value);
			}
		}
		
	}


十,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)

十二,總結

Collection
  *  List(存取有序,有索引,可以重複)
  *    ArrayList
  *     底層是數組實現的,線程不安全,查找和修改快,增和刪比較慢
  *    LinkedList
  *     底層是鏈表實現的,線程不安全,增和刪比較快,查找和修改比較慢
  *    Vector
  *     底層是數組實現的,線程安全的,無論增刪改查都慢
  *    如果查找和修改多,用ArrayList
  *    如果增和刪多,用LinkedList
  *    如果都多,用ArrayList
  *   Set(存取無序,無索引,不可以重複)
  *    HashSet
  *     底層是哈希算法實現
  *     LinkedHashSet
  *      底層是鏈表實現,但是也是可以保證元素唯一,和HashSet原理一樣
  *    TreeSet
  *     底層是二叉樹算法實現
  *    一般在開發的時候不需要對存儲的元素排序,所以在開發的時候大多用HashSet,HashSet的效率比較高
  *    TreeSet在面試的時候比較多,問你有幾種排序方式,和幾種排序方式的區別
  * Map
  *   HashMap
  *    底層是哈希算法,針對鍵
  *    LinkedHashMap
  *     底層是鏈表,針對鍵
  *   TreeMap
  *    底層是二叉樹算法,針對鍵
  *   開發中用HashMap比較多

發佈了46 篇原創文章 · 獲贊 11 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章