Java基礎-18(02)總結Map,HashMap,HashMap與Hashtable區別,Collections工具類
阿新 • • 發佈:2022-05-04
package cn.itcast_07; import java.util.Hashtable; /* * 1:Hashtable和HashMap的區別? * Hashtable:執行緒安全,效率低。不允許null鍵和null值 * HashMap:執行緒不安全,效率高。允許null鍵和null值 * * 2:List,Set,Map等介面是否都繼承子Map介面? * List,Set不是繼承自Map介面,它們繼承自Collection介面 * Map介面本身就是一個頂層介面 */ public class HashtableDemo { public static void main(String[] args) { // HashMap<String, String> hm = new HashMap<String, String>(); Hashtable<String, String> hm = new Hashtable<String, String>(); hm.put("it001", "hello"); // hm.put(null, "world"); //NullPointerException // hm.put("java", null); // NullPointerException System.out.println(hm); } } (9)案例 A:統計一個字串中每個字元出現的次數 package cn.itcast_05; import java.util.Scanner; import java.util.Set; import java.util.TreeMap; /* * 需求 :"aababcabcdabcde",獲取字串中每一個字母出現的次數要求結果:a(5)b(4)c(3)d(2)e(1) * * 分析: * A:定義一個字串(可以改進為鍵盤錄入) * B:定義一個TreeMap集合 * 鍵:Character * 值:Integer * C:把字串轉換為字元陣列 * D:遍歷字元陣列,得到每一個字元 * E:拿剛才得到的字元作為鍵到集合中去找值,看返回值 * 是null:說明該鍵不存在,就把該字元作為鍵,1作為值儲存 * 不是null:說明該鍵存在,就把值加1,然後重寫儲存該鍵和值 * F:定義字串緩衝區變數 * G:遍歷集合,得到鍵和值,進行按照要求拼接 * H:把字串緩衝區轉換為字串輸出 * * 錄入:linqingxia * 結果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1) */ public class TreeMapDemo { public static void main(String[] args) { // 定義一個字串(可以改進為鍵盤錄入) Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字串:"); String line = sc.nextLine(); // 定義一個TreeMap集合 TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>(); //把字串轉換為字元陣列 char[] chs = line.toCharArray(); //遍歷字元陣列,得到每一個字元 for(char ch : chs){ //拿剛才得到的字元作為鍵到集合中去找值,看返回值 Integer i = tm.get(ch); //是null:說明該鍵不存在,就把該字元作為鍵,1作為值儲存 if(i == null){ tm.put(ch, 1); }else { //不是null:說明該鍵存在,就把值加1,然後重寫儲存該鍵和值 i++; tm.put(ch,i); } } //定義字串緩衝區變數 StringBuilder sb= new StringBuilder(); //遍歷集合,得到鍵和值,進行按照要求拼接 Set<Character> set = tm.keySet(); for(Character key : set){ Integer value = tm.get(key); sb.append(key).append("(").append(value).append(")"); } //把字串緩衝區轉換為字串輸出 String result = sb.toString(); System.out.println("result:"+result); } } B:集合的巢狀遍歷 a:HashMap巢狀HashMap package cn.itcast_05; import java.util.HashMap; import java.util.Set; /* * HashMap巢狀HashMap * * 傳智播客 * jc 基礎班 * 陳玉樓 20 * 高躍 22 * jy 就業班 * 李傑 21 * 曹石磊 23 * * 先儲存元素,然後遍歷元素 */ public class HashMapDemo2 { public static void main(String[] args) { // 建立集合物件 HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>(); // 建立基礎班集合物件 HashMap<String, Integer> jcMap = new HashMap<String, Integer>(); // 新增元素 jcMap.put("陳玉樓", 20); jcMap.put("高躍", 22); // 把基礎班新增到大集合 czbkMap.put("jc", jcMap); // 建立就業班集合物件 HashMap<String, Integer> jyMap = new HashMap<String, Integer>(); // 新增元素 jyMap.put("李傑", 21); jyMap.put("曹石磊", 23); // 把基礎班新增到大集合 czbkMap.put("jy", jyMap); //遍歷集合 Set<String> czbkMapSet = czbkMap.keySet(); for(String czbkMapKey : czbkMapSet){ System.out.println(czbkMapKey); HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey); Set<String> czbkMapValueSet = czbkMapValue.keySet(); for(String czbkMapValueKey : czbkMapValueSet){ Integer czbkMapValueValue = czbkMapValue.get(czbkMapValueKey); System.out.println("t"+czbkMapValueKey+"---"+czbkMapValueValue); } } } } b:HashMap巢狀ArrayList package cn.itcast_05; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; /* *需求: *假設HashMap集合的元素是ArrayList。有3個。 *每一個ArrayList集合的值是字串。 *元素我已經完成,請遍歷。 *結果: * 三國演義 * 呂布 * 周瑜 * 笑傲江湖 * 令狐沖 * 林平之 * 神鵰俠侶 * 郭靖 * 楊過 */ public class HashMapIncludeArrayListDemo { public static void main(String[] args) { // 建立集合物件 HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>(); // 建立元素集合1 ArrayList<String> array1 = new ArrayList<String>(); array1.add("呂布"); array1.add("周瑜"); hm.put("三國演義", array1); // 建立元素集合2 ArrayList<String> array2 = new ArrayList<String>(); array2.add("令狐沖"); array2.add("林平之"); hm.put("笑傲江湖", array2); // 建立元素集合3 ArrayList<String> array3 = new ArrayList<String>(); array3.add("郭靖"); array3.add("楊過"); hm.put("神鵰俠侶", array3); //遍歷集合 Set<String> set = hm.keySet(); for(String key : set){ System.out.println(key); ArrayList<String> value = hm.get(key); for(String s : value){ System.out.println("t"+s); } } } } c:ArrayList巢狀HashMap package cn.itcast_05; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; /* ArrayList集合巢狀HashMap集合並遍歷。 需求: 假設ArrayList集合的元素是HashMap。有3個。 每一個HashMap集合的鍵和值都是字串。 元素我已經完成,請遍歷。 結果: 周瑜---小喬 呂布---貂蟬 郭靖---黃蓉 楊過---小龍女 令狐沖---任盈盈 林平之---嶽靈珊 */ public class ArrayListIncludeHashMapDemo { public static void main(String[] args) { // 建立集合物件 ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>(); // 建立元素1 HashMap<String, String> hm1 = new HashMap<String, String>(); hm1.put("周瑜", "小喬"); hm1.put("呂布", "貂蟬"); // 把元素新增到array裡面 array.add(hm1); // 建立元素1 HashMap<String, String> hm2 = new HashMap<String, String>(); hm2.put("郭靖", "黃蓉"); hm2.put("楊過", "小龍女"); // 把元素新增到array裡面 array.add(hm2); // 建立元素1 HashMap<String, String> hm3 = new HashMap<String, String>(); hm3.put("令狐沖", "任盈盈"); hm3.put("林平之", "嶽靈珊"); // 把元素新增到array裡面 array.add(hm3); // 遍歷 for (HashMap<String, String> hm : array) { Set<String> set = hm.keySet(); for (String key : set) { String value = hm.get(key); System.out.println(key + "---" + value); } } } } d:多層巢狀 package cn.itcast_06; public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package cn.itcast_06; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; /* * 為了更符合要求: * 這次的資料就看成是學生物件。 * * 傳智播客 * bj 北京校區 * jc 基礎班 * 林青霞 27 * 風清揚 30 * jy 就業班 * 趙雅芝 28 * 武鑫 29 * sh 上海校區 * jc 基礎班 * 郭美美 20 * 犀利哥 22 * jy 就業班 * 羅玉鳳 21 * 馬徵 23 * gz 廣州校區 * jc 基礎班 * 王力巨集 30 * 李靜磊 32 * jy 就業班 * 郎朗 31 * 柳巖 33 * xa 西安校區 * jc 基礎班 * 范冰冰 27 * 劉意 30 * jy 就業班 * 李冰冰 28 * 張志豪 29 */ public class HashMapDemo { public static void main(String[] args) { // 建立大集合 HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>(); // 北京校區資料 HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array1 = new ArrayList<Student>(); Student s1 = new Student("林青霞", 27); Student s2 = new Student("風清揚", 30); array1.add(s1); array1.add(s2); ArrayList<Student> array2 = new ArrayList<Student>(); Student s3 = new Student("趙雅芝", 28); Student s4 = new Student("武鑫", 29); array2.add(s3); array2.add(s4); bjCzbkMap.put("基礎班", array1); bjCzbkMap.put("就業班", array2); czbkMap.put("北京校區", bjCzbkMap); // 晚上可以自己練習一下 // 上海校區資料 HashMap<String, ArrayList<Student>> shCzbkMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array7 = new ArrayList<Student>(); Student s13 = new Student("郭美美", 20); Student s14 = new Student("犀利哥", 22); array7.add(s13); array7.add(s14); ArrayList<Student> array8 = new ArrayList<Student>(); Student s15 = new Student("羅玉鳳", 21); Student s16 = new Student("馬徵", 23); array8.add(s15); array8.add(s16); shCzbkMap.put("基礎班", array7); shCzbkMap.put("就業班", array8); czbkMap.put("上海校區", shCzbkMap); // 廣州校區資料 HashMap<String, ArrayList<Student>> gzCzbkMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array5 = new ArrayList<Student>(); Student s9 = new Student("王力巨集", 30); Student s10 = new Student("李靜磊", 32); array5.add(s9); array5.add(s10); ArrayList<Student> array6 = new ArrayList<Student>(); Student s11 = new Student("朗朗", 31); Student s12 = new Student("柳巖", 33); array6.add(s11); array6.add(s12); gzCzbkMap.put("基礎班", array5); gzCzbkMap.put("就業班", array6); czbkMap.put("廣州校區", gzCzbkMap); // 西安校區資料 HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array3 = new ArrayList<Student>(); Student s5 = new Student("范冰冰", 27); Student s6 = new Student("劉意", 30); array3.add(s5); array3.add(s6); ArrayList<Student> array4 = new ArrayList<Student>(); Student s7 = new Student("李冰冰", 28); Student s8 = new Student("張志豪", 29); array4.add(s7); array4.add(s8); xaCzbkMap.put("基礎班", array3); xaCzbkMap.put("就業班", array4); czbkMap.put("西安校區", xaCzbkMap); // 遍歷集合 Set<String> czbkMapSet = czbkMap.keySet(); for (String czbkMapKey : czbkMapSet) { System.out.println(czbkMapKey); HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap .get(czbkMapKey); Set<String> czbkMapValueSet = czbkMapValue.keySet(); for (String czbkMapValueKey : czbkMapValueSet) { System.out.println("t" + czbkMapValueKey); ArrayList<Student> czbkMapValueValue = czbkMapValue .get(czbkMapValueKey); for (Student s : czbkMapValueValue) { System.out.println("tt" + s.getName() + "---" + s.getAge()); } } } } } 2:Collections(理解) (1)是針對集合進行操作的工具類 (2)面試題:Collection和Collections的區別 A:Collection 是單列集合的頂層介面,有兩個子介面List和Set B:Collections 是針對集合進行操作的工具類,可以對集合進行排序和查詢等 (3)常見的幾個小方法: A:public static <T> void sort(List<T> list) B:public static <T> int binarySearch(List<?> list,T key) C:public static <T> T max(Collection<?> coll) D:public static void reverse(List<?> list) E:public static void shuffle(List<?> list) package cn.itcast_01; import java.util.Collections; import java.util.List; import java.util.ArrayList; /* * Collections:是針對集合進行操作的工具類,都是靜態方法。 * * 面試題: * Collection和Collections的區別? * Collection:是單列集合的頂層介面,有子介面List和Set。 * 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):隨機置換 */ public class CollectionsDemo { public static void main(String[] args) { // 建立集合物件 List<Integer> list = new ArrayList<Integer>(); // 新增元素 list.add(30); list.add(20); list.add(50); list.add(10); list.add(40); System.out.println("list:" + list); // public static <T> void sort(List<T> list):排序 預設情況下是自然順序。 // Collections.sort(list); // System.out.println("list:" + list); // [10, 20, 30, 40, 50] // public static <T> int binarySearch(List<?> list,T key):二分查詢 // System.out // .println("binarySearch:" + Collections.binarySearch(list, 30)); // System.out.println("binarySearch:" // + Collections.binarySearch(list, 300)); // public static <T> T max(Collection<?> coll):最大值 // System.out.println("max:"+Collections.max(list)); // public static void reverse(List<?> list):反轉 // Collections.reverse(list); // System.out.println("list:" + list); //public static void shuffle(List<?> list):隨機置換 Collections.shuffle(list); System.out.println("list:" + list); } } (4)案例 A:ArrayList集合儲存自定義物件的排序 package cn.itcast_02; /** * @author Administrator * */ public class Student implements Comparable<Student> { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int compareTo(Student s) { int num = this.age - s.age; int num2 = num == 0 ? this.name.compareTo(s.name) : num; return num2; } } package cn.itcast_02; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /* * Collections可以針對ArrayList儲存基本包裝類的元素排序,儲存自定義物件可不可以排序呢? */ public class CollectionsDemo { public static void main(String[] args) { // 建立集合物件 List<Student> list = new ArrayList<Student>(); // 建立學生物件 Student s1 = new Student("林青霞", 27); Student s2 = new Student("風清揚", 30); Student s3 = new Student("劉曉曲", 28); Student s4 = new Student("武鑫", 29); Student s5 = new Student("林青霞", 27); // 新增元素物件 list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); // 排序 // 自然排序 // Collections.sort(list); // 比較器排序 // 如果同時有自然排序和比較器排序,以比較器排序為主 Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s2.getAge() - s1.getAge(); int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; return num2; } }); // 遍歷集合 for (Student s : list) { System.out.println(s.getName() + "---" + s.getAge()); } } } B:模擬鬥地主洗牌和發牌 package cn.itcast_03; import java.util.ArrayList; import java.util.Collections; /* * 模擬鬥地主洗牌和發牌 * * 分析: * A:建立一個牌盒 * B:裝牌 * C:洗牌 * D:發牌 * E:看牌 */ public class PokerDemo { public static void main(String[] args) { // 建立一個牌盒 ArrayList<String> array = new ArrayList<String>(); // 裝牌 // 黑桃A,黑桃2,黑桃3,...黑桃K // 紅桃A,... // 梅花A,... // 方塊A,... // 定義一個花色陣列 String[] colors = { "?", "?", "?", "?" }; // 定義一個點數陣列 String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; // 裝牌 for (String color : colors) { for (String number : numbers) { array.add(color.concat(number)); } } array.add("小王"); array.add("大王"); // 洗牌 Collections.shuffle(array); // System.out.println("array:" + array); // 發牌 ArrayList<String> fengQingYang = new ArrayList<String>(); ArrayList<String> linQingXia = new ArrayList<String>(); ArrayList<String> liuYi = new ArrayList<String>(); ArrayList<String> diPai = new ArrayList<String>(); for (int x = 0; x < array.size(); x++) { if (x >= array.size() - 3) { diPai.add(array.get(x)); } else if (x % 3 == 0) { fengQingYang.add(array.get(x)); } else if (x % 3 == 1) { linQingXia.add(array.get(x)); } else if (x % 3 == 2) { liuYi.add(array.get(x)); } } // 看牌 lookPoker("風清揚", fengQingYang); lookPoker("林青霞", linQingXia); lookPoker("劉意", liuYi); lookPoker("底牌", diPai); } public static void lookPoker(String name, ArrayList<String> array) { System.out.print(name + "的牌是:"); for (String s : array) { System.out.print(s + " "); } System.out.println(); } } C:模擬鬥地主洗牌和發牌並對牌進行排序 package cn.itcast_04; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; /* * 思路: * A:建立一個HashMap集合 * B:建立一個ArrayList集合 * C:建立花色陣列和點數陣列 * D:從0開始往HashMap裡面儲存編號,並存儲對應的牌 * 同時往ArrayList裡面儲存編號即可。 * E:洗牌(洗的是編號) * F:發牌(發的也是編號,為了保證編號是排序的,就建立TreeSet集合接收) * G:看牌(遍歷TreeSet集合,獲取編號,到HashMap集合找對應的牌) */ public class PokerDemo { public static void main(String[] args) { // 建立一個HashMap集合 HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 建立一個ArrayList集合 ArrayList<Integer> array = new ArrayList<Integer>(); // 建立花色陣列和點數陣列 // 定義一個花色陣列 String[] colors = { "?", "?", "?", "?" };黑桃,紅桃,梅花,方片由於txt格式不能顯示所以?代替 // 定義一個點數陣列 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", }; // 從0開始往HashMap裡面儲存編號,並存儲對應的牌,同時往ArrayList裡面儲存編號即可。 int index = 0; for (String number : numbers) { for (String color : colors) { String poker = color.concat(number); hm.put(index, poker); array.add(index); index++; } } hm.put(index, "小王"); array.add(index); index++; hm.put(index, "大王"); array.add(index); // 洗牌(洗的是編號) Collections.shuffle(array); // 發牌(發的也是編號,為了保證編號是排序的,就建立TreeSet集合接收) TreeSet<Integer> fengQingYang = new TreeSet<Integer>(); TreeSet<Integer> linQingXia = new TreeSet<Integer>(); TreeSet<Integer> liuYi = new TreeSet<Integer>(); TreeSet<Integer> diPai = new TreeSet<Integer>(); for (int x = 0; x < array.size(); x++) { if (x >= array.size() - 3) { diPai.add(array.get(x)); } else if (x % 3 == 0) { fengQingYang.add(array.get(x)); } else if (x % 3 == 1) { linQingXia.add(array.get(x)); } else if (x % 3 == 2) { liuYi.add(array.get(x)); } } // 看牌(遍歷TreeSet集合,獲取編號,到HashMap集合找對應的牌) lookPoker("風清揚", fengQingYang, hm); lookPoker("林青霞", linQingXia, hm); lookPoker("劉意", liuYi, hm); lookPoker("底牌", diPai, hm); } // 寫看牌的功能 public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) { System.out.print(name + "的牌是:"); for (Integer key : ts) { String value = hm.get(key); System.out.print(value + " "); } System.out.println(); } }