1. Map集合之基礎增刪查等功能java
1 public class Demo1_Map { 2 3 /* 4 * Map集合是以鍵值對的形式存在,底層依賴的是set集合 5 * 6 * a. 添加功能 7 * V put(K key, V value) 8 * 若是鍵是第一次存入,則就直接存儲元素,返回null (其實本質是和鍵存在是同樣的,只是覆蓋的null) 9 * 若是鍵不是第一次存入,就用值把之前的替換掉,返回之前的值 10 * b. 刪除功能 11 * void clear() 移除全部的鍵值信息 12 * V remove(Object key) 刪除指定鍵對應的元素值,並把值返回,當所傳的鍵值不存在時就返回null 13 * c. 判斷功能 14 * boolean containsKey(Object key) 判斷集合中是否包含此鍵 15 * boolean containsValue(Object value) 判斷集合是否包含指定的值 16 * boolean isEmpty() 判斷集是否爲空 17 * d. 獲取功能 18 * Set<Map.Entry<K,V>> entrySet() 19 * V get(Object key) 根據鍵獲取值 20 * Set<K> keySet() 獲取集合中全部鍵的集合 21 * Collection<V> values() 獲取集合中全部值的集合,返回一個Conllection 22 * e. 長度功能 23 * int size() 返回集合中全部鍵值對的個數 24 * 25 */ 26 public static void main(String[] args) { 27 28 //demo1(); 29 //demo2(); 30 //demo3(); 31 Map<String, Integer> map = new HashMap<>(); 32 Integer i1 = map.put("張三", 23); 33 Integer i2 = map.put("李四", 24); 34 Integer i3 = map.put("王五", 25); 35 Integer i4 = map.put("趙六", 26); 36 Integer i5 = map.put("張三", 26); 37 Set<Entry<String, Integer>> set = map.entrySet(); 38 System.out.println(set); //[李四=24, 張三=26, 王五=25, 趙六=26] 39 System.out.println(map.values()); //[24, 26, 25, 26] 40 41 } 42 43 /** 44 * 判斷功能 45 */ 46 public static void demo3() { 47 Map<String, Integer> map = new HashMap<>(); 48 map.put("lisi", 24); 49 boolean b1 = map.containsKey("lisi"); 50 System.out.println(b1); //true 51 boolean b2 = map.containsValue(24) ; 52 System.out.println(b2); //true 53 } 54 55 /** 56 * 刪除功能 57 */ 58 public static void demo2() { 59 Map<String, Integer> map = new HashMap<>(); 60 map.put("李四", 24); 61 Integer i = map.remove("張三"); 62 System.out.println(i); //null 63 Integer i1 = map.remove("李四"); 64 System.out.println(i1); //24 65 } 66 67 /** 68 * 添加功能 69 */ 70 public static void demo1() { 71 Map<String, Integer> map = new HashMap<>(); 72 Integer i1 = map.put("張三", 23); 73 Integer i2 = map.put("李四", 24); 74 Integer i3 = map.put("王五", 25); 75 Integer i4 = map.put("趙六", 26); 76 Integer i5 = map.put("張三", 26); 77 System.out.println(map); 78 System.out.println(i1); 79 System.out.println(i2); 80 System.out.println(i3); 81 System.out.println(i4); 82 System.out.println(i5); 83 } 84 85 }
2. 遍歷Map集合git
1 public class Demo2_Map { 2 3 /** 4 * @param args 5 * 遍歷map集合 6 * 7 */ 8 public static void main(String[] args) { 9 10 Map<String, Integer> map = new HashMap<>(); 11 map.put("張三", 23); 12 map.put("李四", 24); 13 map.put("王五", 25); 14 map.put("趙六", 26); 15 16 /* 17 使用迭代器進行遍歷Map集合 18 Set<String> set = map.keySet(); 19 Iterator<String> it = set.iterator(); 20 while (it.hasNext()) { 21 String key = it.next(); 22 System.out.println(key + "=" + map.get(key)); 23 }*/ 24 25 26 //使用加強for循環進行遍歷Map集合 27 for (String string : map.keySet()) { 28 System.out.println(string + "=" + map.get(string)); 29 } 30 } 31 32 }
3. 經過Entry對Map集合進行遍歷面試
1 public class Demo3_MapEntry { 2 3 /* 4 * 經過Entry對Map集合進行遍歷 5 * Entry中後獲取鍵的方法getKey() 和 獲取值的方法 getValue() 6 */ 7 public static void main(String[] args) { 8 9 Map<String, Integer> map = new HashMap<>(); 10 map.put("張三", 23); 11 map.put("李四", 24); 12 map.put("王五", 25); 13 map.put("趙六", 26); 14 15 /* 16 *使用迭代器遍歷集合 17 Set<Map.Entry<String, Integer>> entry = map.entrySet(); 18 Iterator<Map.Entry<String, Integer>> it = entry.iterator(); 19 while (it.hasNext()) { 20 Map.Entry<String, Integer> en = it.next(); 21 String key = en.getKey(); 22 Integer value = en.getValue(); 23 System.out.println(key + "=" + value); 24 }*/ 25 26 27 //經過加強for循環遍歷集合 28 for (Map.Entry<String, Integer> en : map.entrySet()) { 29 System.out.println(en.getKey() + "=" + en.getValue()); 30 } 31 } 32 33 }
4. HashMap算法
1 public class Demo4_HashMap { 2 3 /* 4 * 向Map集合中存入自定義對象,將自定義對象做爲集合的key 5 * 6 */ 7 public static void main(String[] args) { 8 Map<Student, String> map = new HashMap<>(); 9 map.put(new Student("張三",23), "北京"); 10 map.put(new Student("張三",23), "上海"); 11 map.put(new Student("李四",24), "廣州"); 12 //重寫hashCode() 和 equals() 方法前 {Student [name=張三, age=23]=北京, Student [name=李四, age=24]=廣州, Student [name=張三, age=23]=上海} 13 //System.out.println(map); 14 //重寫hashCode() 和 equals() 方法後 {Student [name=張三, age=23]=上海, Student [name=李四, age=24]=廣州} 15 System.out.println(map); 16 17 } 18 19 }
5. LinkedHashMap 怎麼存就怎麼取數組
1 public class Demo5_LinkedHashMap { 2 3 /* 4 * LinkedHashMap 怎麼存就怎麼取 5 */ 6 public static void main(String[] args) { 7 8 LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>(); 9 lhm.put("張三", 23); 10 lhm.put("趙六", 26); 11 lhm.put("王五", 25); 12 lhm.put("李四", 24); 13 System.out.println(lhm); //{張三=23, 趙六=26, 王五=25, 李四=24} 14 } 15 16 }
6. TreeMap安全
1 public class Demo6_TreeMap { 2 3 /* 4 * 通TreeSet同樣,也是有兩種排序方式: 5 * 1. 使用對象本身進行排序 6 * 2. 使用比較器進行排序 7 */ 8 public static void main(String[] args) { 9 //demo2(); 10 //demo1(); 11 12 /* 13 * TreeMap傳入Comparator對象,重寫compare方法,先比較姓名,再比較年齡 14 */ 15 TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() { 16 17 @Override 18 public int compare(Student s1, Student s2) { 19 int num = s1.getName().compareTo(s2.getName()); 20 return num == 0 ? s1.getAge() - s2.getAge() : num; 21 } 22 }); 23 tm.put(new Student("張三",23), "北京"); 24 tm.put(new Student("李四", 24),"上海"); 25 tm.put(new Student("趙六", 26),"廣州"); 26 tm.put(new Student("王五", 25),"深圳"); 27 System.out.println(tm); 28 //{Student [name=張三, age=23]=北京, Student [name=李四, age=24]=上海, Student [name=王五, age=25]=深圳, Student [name=趙六, age=26]=廣州} 29 30 } 31 32 /** 33 * 普通的引用數據類型 34 */ 35 public static void demo2() { 36 TreeMap<String, Integer> ts = new TreeMap<>(); 37 ts.put("張三", 23); 38 ts.put("王五", 25); 39 ts.put("李四", 24); 40 ts.put("趙六", 26); 41 System.out.println(ts); //{張三=23, 李四=24, 王五=25, 趙六=26} 42 } 43 44 /** 45 * 自定義類實現Comparator接口,重寫compareTo()方法,先比較年齡,後比較姓名 46 */ 47 public static void demo1() { 48 TreeMap<Student, String> tm = new TreeMap<>(); 49 tm.put(new Student("張三",23), "北京"); 50 tm.put(new Student("李四", 24),"上海"); 51 tm.put(new Student("趙六", 26),"廣州"); 52 tm.put(new Student("王五", 25),"深圳"); 53 System.out.println(tm); 54 //當自定義類沒有實現comparable時直接添加會報java.lang.ClassCastException: com.map.Student cannot be cast to java.lang.Comparable 55 //{Student [name=張三, age=23]=北京, Student [name=李四, age=24]=上海, Student [name=王五, age=25]=深圳, Student [name=趙六, age=26]=廣州} 56 } 57 58 }
6.1 Student 自定義類ide
1 public class Student implements Comparable<Student> { 2 3 private String name; 4 private int age; 5 6 public Student() { 7 super(); 8 } 9 10 public Student(String name, int age) { 11 super(); 12 this.name = name; 13 this.age = age; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public int getAge() { 25 return age; 26 } 27 28 public void setAge(int age) { 29 this.age = age; 30 } 31 32 @Override 33 public String toString() { 34 return "Student [name=" + name + ", age=" + age + "]"; 35 } 36 37 @Override 38 public int hashCode() { 39 final int prime = 31; 40 int result = 1; 41 result = prime * result + age; 42 result = prime * result + ((name == null) ? 0 : name.hashCode()); 43 return result; 44 } 45 46 @Override 47 public boolean equals(Object obj) { 48 if (this == obj) 49 return true; 50 if (obj == null) 51 return false; 52 if (getClass() != obj.getClass()) 53 return false; 54 Student other = (Student) obj; 55 if (age != other.age) 56 return false; 57 if (name == null) { 58 if (other.name != null) 59 return false; 60 } else if (!name.equals(other.name)) 61 return false; 62 return true; 63 } 64 65 @Override 66 public int compareTo(Student o) { 67 int num = this.age - o.age; //優先比對年齡 68 return num == 0 ? this.name.compareTo(o.name) : num; 69 } 70 71 }
7. HashMap和Hashtable的區別
* 共同點:
* 底層都是哈希算法,都是雙列集合
* 區別:
* 1. HashMap是線程不安全的,效率高,JDK 1.2 版本
* Hashtable 是線程安全的,效率低,JDK 1.0版本
* 2. HashMap能夠存儲null鍵和null值
* Hashtable不能夠函數
8. 集合練習之實現鬥地主的得到牌、洗牌、發牌this
* 需求:實現鬥地主的洗牌、發牌、看牌(按所拿的牌排序顯示)
* 分析:
* 1 得到一副牌
* 2 將牌存入HashMap集合中,將每張牌對應的索引存入list集合中
* 3 根據list集合中存的索引對牌進行洗牌
* 4 根據洗完牌的索引進行發牌
* 5 對每一個人發的牌及底牌進行查看spa
1 public class Demo9_Doudizhu { 2 3 public static void main(String[] args) { 4 5 //得到一副牌並將牌存入HashMap集合,將索引存入list集合 6 String[] digit = {"2", "4", "5","6", "7","8","9","10","A","J","Q","k","3"}; 7 String[] color = {"紅桃","方片","梅花","黑桃"}; 8 9 HashMap<Integer, String> hm = new HashMap<>(); 10 List<Integer> list = new ArrayList<>(); 11 int index = 0; 12 for(String s1 : digit) { 13 for(String s2 : color) { 14 hm.put(index, s2.concat(s1)); 15 list.add(index); 16 index++; 17 } 18 } 19 hm.put(index, "小王"); 20 list.add(index); 21 index++; 22 hm.put(index,"大王"); 23 list.add(index); 24 25 //洗牌 26 Collections.shuffle(list); 27 28 //發牌,建立相應的TreeSet集合用於存儲發個每一個人的牌和底牌 29 TreeSet<Integer> gaojin = new TreeSet<>(); 30 TreeSet<Integer> longwu = new TreeSet<>(); 31 TreeSet<Integer> me = new TreeSet<>(); 32 TreeSet<Integer> dipai = new TreeSet<>(); 33 for (Integer i : list) { 34 if(i >= list.size() - 3) { 35 dipai.add(list.get(i)); 36 }else if(i % 3 == 0) { 37 gaojin.add(list.get(i)); 38 }else if(i %3 == 1){ 39 longwu.add(list.get(i)); 40 }else { 41 me.add(list.get(i)); 42 } 43 } 44 45 //看牌 46 lookPoker(hm, gaojin, "高進"); 47 lookPoker(hm, longwu, "龍武"); 48 lookPoker(hm, me, "我"); 49 lookPoker(hm, dipai, "底牌"); 50 51 } 52 53 /** 54 * @param hm 55 * @param ts 56 * @param name 57 * 看牌函數 58 */ 59 public static void lookPoker(HashMap<Integer, String> hm, TreeSet<Integer> ts, String name) { 60 System.out.print(name + "的牌是:"); 61 for (Integer i : ts) { 62 System.out.print(hm.get(i) + " "); 63 } 64 System.out.println(); 65 } 66 67 }
9. 集合相關總結
* Collectiion * List(存取有序,有索引,能夠重複) * ArrayList * 底層是數組實現的,線程不安全,查找和修改快,增長和刪除慢 * LinkedList * 底層是鏈表實現的,線程不安全,增和刪快,查和修改慢 * Vector * 底層是數組實現的,線程是安全的,不管增刪改查都滿 * 若是查找和修改多,用ArrayList * 若是增和刪多,用LinkedList * 若是都多,用ArrayList * Set(存儲無序,無索引,不可重複) * HashSet * 底層是哈希算法實現的 * LinkedHashSet * 底層是鏈表實現的,可是也是能夠保證元素惟一,和HashSet原理同樣 * TreeSet * 底層是二叉樹算法實現的 * 通常在開發的時候不須要對存儲的元素排序,因此在開發的通常用HashSet,HashSet的效率比較高 * TreeSet在面試的時候比較多,問你有幾種排序方式,和幾種排序方式的區別 * Map * HashMap * 底層是哈希算法,針對鍵 * LinkedHashMap * 底層是鏈表,針對鍵 * TreeMap * 底層是二叉樹算法,針對鍵 * 開發中用HashMap比較多