java 集合Map

來源:互聯網
上載者:User

標籤:nta   object   sys   hashmap   contain   col   個數   ati   nsvalue   

1. Map集合

特點:將鍵映射到值的對象,一個映射不能包含重複的鍵; 每個鍵可以映射到最多一個值。無序。

Map 與 Collection集合的區別

Map集合儲存元素成對出現,雙列,Collection是單列的

Map的鍵是唯一的,Collection 的子體系Set是唯一的

Map集合的資料結構針對鍵有效,與值無關

Collection的集合的資料結構只針對元素有效

功能概述:

1. 添加功能

V put(K key, V value):添加(修改)功能: 如果該鍵已經存在,則將值替換,並返回前一個鍵對應的值,第一次儲存,返回Null

2. 刪除功能:

void clear():移除所有的索引值對元素

V remove(Object key):根據索引值對刪除元素,並把值返回

3. 判斷功能

boolean containsKey(Object key):判斷集合是否包含指定的鍵

boolean containsValue(Object value):判斷集合是否包含指定的值

boolean isEmpty():判斷集合是否為空白

4. 擷取功能

Set<Map,Entry<K,V>> entrySet():返回索引值對對象

V get(Object key):根據鍵擷取值

set<E> keySet():擷取集合中所有鍵的集合

Collection<V> values():擷取集合中所有值的集合

5. 長度功能

int size():返回集合索引值對的個數

測試:

    public static void main(String[] args) {        Map<String, String> map = new HashMap<String, String>();        System.out.println("put:" + map.put("文章", "馬伊利"));        System.out.println("put:" + map.put("文章", "姚笛")); // 如果該鍵已經存在,則將值修改,並返回前一個鍵對應的值        map.put("wangwang", "xiaojingzi");        map.put("wangzi", "xiaojunzi");        map.put("hage", "xiaoha");        System.out.println("map:" + map);        System.out.println(map.remove("wangwang")); // 返回鍵所對應的值:"xiaojingzi"        System.out.println(map.containsKey("wangzi")); // 存在返回true,不存在返回false        System.out.println(map.size());                System.out.println(map.get("文章")); // 根據鍵傳回值,不存在返回Null        Set<String> set = map.keySet();        // 返回集合中所有鍵的結合        for (String key : set) {            System.out.println(key);        }        Collection<String> con = map.values();    // 返回集合中所有值的集合        for (String value : con) {            System.out.println(value);        }                for(String key : set) {                    // map集合的遍曆方式1            System.out.println(key + "=" + map.get(key));        }                Set<Map.Entry<String, String>> set1 = map.entrySet();  // map集合的遍曆方式2        for(Map.Entry<String, String> me : set1) {            System.out.println(me.getKey() + ":" + me.getValue());        }    }

2. HashMap類

特點:鍵是雜湊表的結構,可以保證鍵的唯一性

 

java 集合Map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.