1. 程式人生 > 實用技巧 >集合-----雙列

集合-----雙列

Map

雙列集合圖解:

Map集合概述:

  • Interface Map<K,V> K :鍵的資料型別; V :值得資料型別
  • 鍵不能重複,值可以重複
  • 鍵和值是一一對應的,通過鍵只能找到自己對應的值
  • 每一對鍵值被稱為鍵值對或者Entry物件

Map集合簡單新增程式碼如下:

  public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("001", "小美");
        String s1 = map.put("002", "小明");
        map.put(
"003", "小剛"); map.put("004", "小強"); map.put("005", "大壯"); System.out.println(s1); String s = map.put("001", "大壯");//如果新增的元素的鍵值已經存在那麼 // 會將原來的值替換成新的值,並將原來的值返回, // 如果新增的鍵值不存在,則直接新增,返回值為null System.out.println(s); System.out.println(map); }

執行結果如下:

Map集合的刪除程式碼如下:

        Map<String, String> map = new HashMap<>();
        map.put("001", "小美");
        map.put("002", "小明");
        map.put("003", "小剛");
        map.put("004", "小強");
        map.put("005", "大壯");
        String s = map.remove("001");//返回刪除元素的值
        System.out.println(s);
        System.out.println(map);


    }

執行結果如下:

Map集合移除所有元素的方法:

 1        Map<String, String> map = new HashMap<>();
 2         map.put("001", "小美");
 3         map.put("002", "小明");
 4         map.put("003", "小剛");
 5         map.put("004", "小強");
 6         map.put("005", "大壯");
 7       map.clear();//刪除所有鍵值對物件
 8         System.out.println(map);
 9 
10 
11     }

執行結果如下:

Map集合判斷是否存在指定Key值:

程式碼如下:

 1     public static void main(String[] args) {
 2         Map<String, String> map = new HashMap<>();
 3         map.put("001", "小美");
 4         map.put("002", "小明");
 5         map.put("003", "小剛");
 6         map.put("004", "小強");
 7         map.put("005", "大壯");
 8         boolean b1= map.containsKey("001");//判斷集合中是否存在指定的Key值,並返回boolean值
 9         boolean b2 = map.containsKey("009");
10         System.out.println(b1);
11         System.out.println(b2);
12 
13     }

執行結果:

Map集合判斷是否存在指定值:

程式碼如下:

 1   public static void main(String[] args) {
 2         Map<String, String> map = new HashMap<>();
 3         map.put("001", "小美");
 4         map.put("002", "小明");
 5         map.put("003", "小剛");
 6         map.put("004", "小強");
 7         map.put("005", "大壯");
 8         boolean b1 = map.containsValue("小美");
 9         boolean b2 = map.containsValue("小小");//判斷是否存在指定的值,並返回boolean值
10         System.out.println(b1);
11         System.out.println(b2);
12 
13     }

執行結果:

Map集合判斷集合是否為空

程式碼如下:

 1  public static void main(String[] args) {
 2         Map<String, String> map = new HashMap<>();
 3         map.put("001", "小美");
 4         map.put("002", "小明");
 5         map.put("003", "小剛");
 6         map.put("004", "小強");
 7         map.put("005", "大壯");
 8         boolean empty = map.isEmpty();//判斷Map集合是否為空
 9         map.clear();
10         boolean empty1 = map.isEmpty();
11         System.out.println(empty);
12         System.out.println(empty1);
13 
14     }

執行結果:

獲得Map集合的鍵值對個數

程式碼如下:

1       Map<String, String> map = new HashMap<>();
2         map.put("001", "小美");
3         map.put("002", "小明");
4         map.put("003", "小剛");
5         map.put("004", "小強");
6         map.put("005", "大壯");
7         int size = map.size();//獲得Map集合的鍵值對個數
8         System.out.println(size);
9     }

執行結果:

Map集合的兩種遍歷方式:

第一種:使用keySet();方法獲得儲存所有鍵的Set集合然後用get();方法獲得制定鍵對應的值

程式碼如下:

 1     public static void main(String[] args) {
 2         Map<String, String> map = new HashMap<>();
 3         map.put("001", "小美");
 4         map.put("002", "小明");
 5         map.put("003", "小剛");
 6         map.put("004", "小強");
 7         map.put("005", "大壯");
 8         Set<String> set = map.keySet();//keySet();獲得所有的鍵的Set集合
 9         for (String s : set) {
10             String s1 = map.get(s);//get();方法獲得指定鍵的值
11             System.out.println(s+"     "+s1);
12         }
13     }

執行結果為:

第二種:首先使用entrySet();方法獲得所有的鍵值對物件,然後根據物件使用getKey();和

getValue();方法獲得鍵和值;

程式碼如下:

 1  public static void main(String[] args) {
 2         Map<String, String> map = new HashMap<>();
 3         map.put("001", "小美");
 4         map.put("002", "小明");
 5         map.put("003", "小剛");
 6         map.put("004", "小強");
 7         map.put("005", "大壯");
 8         Set<Map.Entry<String, String>> entries = map.entrySet();//獲得所有的鍵值對物件
 9         for (Map.Entry<String, String> entry : entries) {
10             String key = entry.getKey();//獲得key值
11             String value = entry.getValue();//獲得Value值
12             System.out.println(key+"    "+value);
13         }
14     }

結果如下:

HashMap(底層是hash表結構)

(hash表在資料結構中解釋)

HashMap集合如果要儲存自定義類物件必須要重寫equals();和hashCode();方法

TreMap(底層是紅黑樹)

(紅黑樹在資料結構中解釋)

TreeMap集合自定義物件要使用自然排序或者比較器排序重寫其中的方法。只對鍵進行排序