【Java】如何理解Java中的双列集合Map?
1 Map<K,V>接口
1.1 特點
1.2 常用方法
public V put(K key, V value): 不重復返回null,重復返回value
public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);Integer out1 = persons.put("李四", 20);Integer out2 = persons.put("李四", 30);System.out.println(out1);//不重復返回nullSystem.out.println(out2);//重復返回原來的value=20System.out.println(persons);//{李四=30, 張三=19}}public V remove(Object Key):指定鍵,刪除鍵值對,返回刪除鍵對應的值,鍵不存在則返回null
public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);persons.put("李四", 20);Integer out1 = persons.remove("張三");System.out.println(persons);//{李四=20}System.out.println(out1);//19Integer out2 = persons.remove("王五");System.out.println(out2);//null}public V get(Object Key):通過鍵來獲取值
public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);System.out.println(persons.get("張三"));//19System.out.println(persons.get("李四"));//null}public boolean containsKey(Object Key):鍵是否存在
public static void main(String[] args) {Map<String, Integer> persons = new HashMap<>();persons.put("張三",19);System.out.println(persons.containsKey("張三"));//trueSystem.out.println(persons.containsKey("李四"));//false}1.3 遍歷Map集合
方法1:遍歷鍵找值法
public static void main(String[] args) {Map<String, Integer> map = new HashMap<>();map.put("張三",19);map.put("李四",20);map.put("王五",21);//1. 取出Map的所有key存在Set中Set<String> set = map.keySet();//2.1 while遍歷set,獲取key,map.get獲取valueIterator<String> it = set.iterator();while(it.hasNext()){String key = it.next();Integer value = map.get(key);System.out.println(key + " " + value);}//2.2 foreach法遍歷for(String key: set){System.out.println(key + " " + map.get(key));}}方法2: Entry鍵值對對象
Map接口中有一個內部接口Entry
作用:Map一創建,就在Map中創建一個Entry對象,用來記錄鍵和值,得到二者的映射關系。(相當于結婚證)
2 Map接口的實現類—— HashMap
2.1 特點
2.2 存儲自定義類型鍵值
注意:因為Map集合要保證Key是唯一的,所以作為Key的元素必須重寫hashCode方法和equals方法,從而保證Key的唯一性。
public static void main(String[] args) {Map<Person, Integer> map1 = new HashMap<Person, Integer>();map1.put(new Person("李四",10),170);map1.put(new Person("李四",10),180);System.out.println(map1);//重寫前輸出:{Person{name='李四', age=10}=180, Person{name='李四', age=10}=170}//重寫后輸出:{Person{name='李四', age=10}=180}}3 HashMap的子類——LinkedHashMap
3.1 特點
3.2 使用示例
public static void main(String[] args) {HashMap<String, Integer> map1 = new HashMap<String, Integer>();map1.put("a",1);map1.put("c",3);map1.put("b",2);System.out.println(map1);//存和取順序不同 {a=1, b=2, c=3}LinkedHashMap<String, Integer> map2 = new LinkedHashMap<>();map2.put("a",1);map2.put("c",3);map2.put("b",2);System.out.println(map2);//存和取順序相同{a=1, c=3, b=2}}4 Map接口的實現類——Hashtable
4.1 Hashtable的特點
4.2 示例
public static void main(String[] args) {Hashtable<String,String> ht = new Hashtable<>();HashMap<String, String> hm = new HashMap<>();hm.put(null,null);hm.put("a",null);hm.put(null,"b");System.out.println(hm);//{null=b, a=null} // ht.put(null,null); // 報錯:java.lang.NullPointerException}5 Map集合練習
5.1 統計字符出現次數
遍歷字符串:charAt加索引遍歷 / toCharArray轉為數組
for(int i = 0; i < str.length(); i++) —> str.charAt(i)
for(char c: str.toCharArray()) —> c
5.2 斗地主升級版-玩家牌有序
public static void main(String[] args) {//1. 準備牌面String[] colors = {"?","?","?","?"};String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};HashMap<Integer, String> pocker = new HashMap<>();pocker.put(0,"BK");pocker.put(1,"SK");int count = 2;for(String num: nums){for(String color: colors){pocker.put(count++,color+num);}}System.out.println(pocker);//2. 洗牌:打亂編號Set<Integer> set = pocker.keySet();ArrayList<Integer> list = new ArrayList<>();list.addAll(set);Collections.shuffle(list);ArrayList<Integer> left = new ArrayList<>();ArrayList<Integer> p1 = new ArrayList<>();ArrayList<Integer> p2 = new ArrayList<>();ArrayList<Integer> p3 = new ArrayList<>();for (int i = 0; i < pocker.size(); i++) {Integer p = list.get(i);if(i>=51){left.add(p);}else{if(i%3 == 0){p1.add(p);}else if(i%3 == 1){p2.add(p);}else{p3.add(p);}}}//3. 發牌:對編號排序 取出標號對應的值Collections.sort(p1);Collections.sort(p2);Collections.sort(p3);Collections.sort(left);ArrayList<String> player1 = new ArrayList<String>();ArrayList<String> player2 = new ArrayList<String>();ArrayList<String> player3 = new ArrayList<String>();ArrayList<String> dipai = new ArrayList<String>();for(Integer i : p1){player1.add(pocker.get(i));}for(Integer i : p2){player2.add(pocker.get(i));}for(Integer i : p3){player3.add(pocker.get(i));}for(Integer i : left){dipai.add(pocker.get(i));}//4. 看牌System.out.println(player1);System.out.println(player2);System.out.println(player3);System.out.println(dipai);}6 JDK9對集合添加的優化
Java9中添加了幾種集合工廠方法,更方便創建少量元素的集合、map實例。新的List、Set、Map的靜態工廠方法可以更方便地創建集合的不可變實例。
使用前提:集合中存儲的元素個數已經確定,不再改變時使用
注意:
總結
以上是生活随笔為你收集整理的【Java】如何理解Java中的双列集合Map?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端开发者的福音!通过拖拽就可生成Vue
- 下一篇: Nature发布第一张人类造血干细胞发育