java-map類


雙列集合:
-------------| Map 如果是實現了Map接口的集合類,具備的特點: 存儲的數據都是以鍵值對的形式存在的,鍵不可重復,值可以重復。
----------------| HashMap
----------------| TreeMap
----------------| Hashtable

Map接口的方法:
添加

  1. put(K key, V value)
  2. putAll(Map<? extends K,? extends V> m)


刪除

  1. remove(Object key)
  2. clear()

獲取

  1. get(Object key)
  2. size()

判斷

  1. containsKey(Object key)
  2. containsValue(Object value)

isEmpty()

public class Demo2 {    
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        //添加方法
        map.put("汪峰", "章子怡");
        map.put("文章", "馬伊琍");
        map.put("謝霆鋒","張柏芝");
        /*
        添加
        System.out.println("返回值:"+map.put("謝霆鋒","黃菲"));  // 如果之前沒有存在該鍵,那么返回的是null,如果之前就已經存在該鍵了,那么就返回該鍵之前對應 的值。
        Map<String,String> map2 = new HashMap<String, String>();
        map2.put("楊振寧", "翁帆");
        map2.put("習總", "彭麗媛");
        map.putAll(map2); // 把map2的元素添加到map集合中。
        
        */
        
        /*
        刪除
        System.out.println("刪除的數據是:"+map.remove("汪峰")) ;  //根據鍵刪除一條map中的數據,返回的是該鍵對應 的值。
        map.clear(); //清空集合中的所有數據。
        */
        
        /* 獲取
        System.out.println("根據指定 的鍵獲取對應的值:"+ map.get("文章"));
        System.out.println("獲取map集合鍵值對個數:"+map.size());
        
        
        判斷
        System.out.println("判斷map集合是否包含指定的鍵:"+ map.containsKey("文章"));
        System.out.println("判斷map集合中是否包含指定 的值:"+ map.containsValue("張柏芝"));
        map.clear();
        System.out.println("判斷map集合是否為空元素:"+ map.isEmpty());
        */
        System.out.println("集合的元素:"+ map);
    }
}

一、實現類HasMap:底層也是基於哈希表實現的

  往HashMap添加元素的時候,首先會調用鍵的hashCode方法得到元素 的哈希碼值,然后經過運算就可以算出該

元素在哈希表中的存儲位置。
  情況1: 如果算出的位置目前沒有任何元素存儲,那么該元素可以直接添加到哈希表中。

  情況2:如果算出 的位置目前已經存在其他的元素,那么還會調用該元素的equals方法與這個位置上的元素進行比較,如果equals方法返回 的是false,那么該元素允許被存儲,如果equals方法返回的是true,那么該元素被視為重復元素,不允存儲。

  

 1 class Emp {
 2     
 3     String name;
 4     
 5     int salary;
 6 
 7     public Emp(String name, int salary) {
 8         super();
 9         this.name = name;
10         this.salary = salary;
11     }
12     
13     
14     @Override
15     public String toString() {
16         return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
17     }
18 
19     @Override
20     public int compareTo(Emp o) {
21         return this.salary - o.salary;
22     }
23     
24 }
25 public class Demo6 {
26     
27     public static void main(String[] args) {
28     /*    TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
29         tree.put('c',10);
30         tree.put('b',2);
31         tree.put('a',5);
32         tree.put('h',12);
33         System.out.println(tree);*/
34         
35         
36         
37         TreeMap<Emp, String> tree = new TreeMap<Emp, String>();
38         tree.put(new Emp("冰冰", 2000),"001");
39         tree.put(new Emp("家寶", 1000),"002");
40         tree.put(new Emp("習總", 3000),"003");
41         tree.put(new Emp("克強", 5000),"005");
42         System.out.println(tree);
43         
44         
45         
46         
47     }
48 
49 }

二、實現類TreeMap TreeMap也是基於紅黑樹(二叉樹)數據結構實現 的, 特點:會對元素的鍵進行排序存儲。

TreeMap 要注意的事項:
1. 往TreeMap添加元素的時候,如果元素的鍵具備自然順序,那么就會按照鍵的自然順序特性進行排序存儲。
2. 往TreeMap添加元素的時候,如果元素的鍵不具備自然順序特性, 那么鍵所屬的類必須要實現Comparable接口,把鍵
的比較規則定義在CompareTo方法上。

3. 往TreeMap添加元素的時候,如果元素的鍵不具備自然順序特性,而且鍵所屬的類也沒有實現Comparable接口,那么就必須
在創建TreeMap對象的時候傳入比較器

 1 class Emp {//implements Comparable<Emp>{
 2     
 3     String name;
 4     
 5     int salary;
 6 
 7     public Emp(String name, int salary) {
 8         super();
 9         this.name = name;
10         this.salary = salary;
11     }
12     
13     
14     @Override
15     public String toString() {
16         return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
17     }
18 
19 /*
20     @Override
21     public int compareTo(Emp o) {
22         return this.salary - o.salary;
23     }*/
24     
25 }
26 
27 
28 //自定義一個比較器
29 class MyComparator implements Comparator<Emp>{
30 
31     @Override
32     public int compare(Emp o1, Emp o2) {
33         return o1.salary - o2.salary;
34     }
35     
36 }
37 
38 
39 
40 
41 public class Demo6 {
42     
43     public static void main(String[] args) {
44     /*    TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
45         tree.put('c',10);
46         tree.put('b',2);
47         tree.put('a',5);
48         tree.put('h',12);
49         System.out.println(tree);*/
50         
51         //創建一個自定義比較器
52         MyComparator comparator = new MyComparator();
53         
54         TreeMap<Emp, String> tree = new TreeMap<Emp, String>(comparator);
55         tree.put(new Emp("冰冰", 2000),"001");
56         tree.put(new Emp("家寶", 1000),"002");
57         tree.put(new Emp("習總", 3000),"003");
58         tree.put(new Emp("克強", 5000),"005");
59         
60         tree.put(new Emp("財厚", 5000),"008");
61         System.out.println(tree);
62         
63         
64         
65         
66     }
67 
68 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM