A:Map接口概述
* 查看API可以知道:
* 將鍵映射到值的對象
* 一個映射不能包含重複的鍵
* 每個鍵最多隻能映射到一個值
* B:Map接口和Collection接口的不同
* Map是雙列的,Collection是單列的
* Map的鍵唯一,Collection的子體系Set是唯一的
* Map集合的數據結構值針對鍵有效,跟值無關;Collection集合的數據結構是針對元素有效
18.02_集合框架(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<K> keySet():獲取集合中所有鍵的集合
* Collection<V> values():獲取集合中所有值的集合
* e:長度功能
* int size():返回集合中的鍵值對的個數
18.03_集合框架(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));
}
**#**重點內容**18.04_集合框架(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());
}
18.08_集合框架(統計字符串中每個字符出現的次數)
·* 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));
}
18.10_集合框架(HashMap和Hashtable的區別)
* A:面試題
* HashMap和Hashtable的區別
* Hashtable是JDK1.0版本出現的,是線程安全的,效率低,HashMap是JDK1.2版本出現的,是線程不安全的,效率高
* Hashtable不可以存儲null鍵和null值,HashMap可以存儲null鍵和null值
18.11_集合框架(Collections工具類的概述和常見方法講解)
- A:Collections類概述
- 針對集合操作 的工具類
- B:Collections成員方法
-
public static void sort(List list)
public static int binarySearch(List
18.12_集合框架(模擬鬥地主洗牌和發牌)
A:案例演示
模擬鬥地主洗牌和發牌,牌沒有排序
//買一副撲克 String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] color = {"方片","梅花","紅桃","黑桃"}; ArrayList<String> poker = new ArrayList<>(); for(String s1 : color) { for(String s2 : num) { poker.add(s1.concat(s2)); } } poker.add("小王"); poker.add("大王"); //洗牌 Collections.shuffle(poker); //發牌 ArrayList<String> gaojin = new ArrayList<>(); ArrayList<String> longwu = new ArrayList<>(); ArrayList<String> me = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); for(int i = 0; i < poker.size(); i++) { if(i >= poker.size() - 3) { dipai.add(poker.get(i)); }else if(i % 3 == 0) { gaojin.add(poker.get(i)); }else if(i % 3 == 1) { longwu.add(poker.get(i)); }else { me.add(poker.get(i)); } } //看牌 System.out.println(gaojin); System.out.println(longwu); System.out.println(me); System.out.println(dipai);
18.14_集合框架(模擬鬥地主洗牌和發牌並對牌進行排序的代碼實現)
- A:案例演示
- 模擬鬥地主洗牌和發牌並對牌進行排序的代碼實現
//買一副牌 String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] color = {"方片","梅花","紅桃","黑桃"}; HashMap<Integer, String> hm = new HashMap<>(); //存儲索引和撲克牌 ArrayList<Integer> list = new ArrayList<>(); //存儲索引 int index = 0; //索引的開始值 for(String s1 : num) { for(String s2 : color) { hm.put(index, s2.concat(s1)); //將索引和撲克牌添加到HashMap中 list.add(index); //將索引添加到ArrayList集合中 index++; } } hm.put(index, "小王"); list.add(index); index++; hm.put(index, "大王"); list.add(index); //洗牌 Collections.shuffle(list); //發牌 TreeSet<Integer> gaojin = new TreeSet<>(); TreeSet<Integer> longwu = new TreeSet<>(); TreeSet<Integer> me = new TreeSet<>(); TreeSet<Integer> dipai = new TreeSet<>(); for(int i = 0; i < list.size(); i++) { if(i >= list.size() - 3) { dipai.add(list.get(i)); //將list集合中的索引添加到TreeSet集合中會自動排序 }else if(i % 3 == 0) { gaojin.add(list.get(i)); }else if(i % 3 == 1) { longwu.add(list.get(i)); }else { me.add(list.get(i)); } } //看牌 lookPoker("高進", gaojin, hm); lookPoker("龍五", longwu, hm); lookPoker("馮佳", me, hm); lookPoker("底牌", dipai, hm); } public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer, String> hm) { System.out.print(name + "的牌是:"); for (Integer index : ts) { System.out.print(hm.get(index) + " "); } System.out.println(); }