久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      Java中Map集合體系的基本使用和常用API

      本篇文章給大家?guī)?lái)了關(guān)于java的相關(guān)知識(shí),其中主要介紹了關(guān)于Map集合體系的基本使用以及常用API的相關(guān)內(nèi)容,下面一起來(lái)看一下,希望對(duì)大家有幫助。

      Java中Map集合體系的基本使用和常用API

      Map集合概述和使用

      Map集合是一種雙列集合,每個(gè)元素包含兩個(gè)數(shù)據(jù)。

      Map集合的每個(gè)元素的格式:key=value(鍵值對(duì)元素)。

      Map集合也被稱(chēng)為“鍵值對(duì)集合”。

      Map集合整體格式:

      Collection集合的格式: [元素1,元素2,元素3..]

      Map集合的完整格式:{key1=value1 , key2=value2 , key3=value3 , ...}

      Map集合的使用場(chǎng)景之一:購(gòu)物車(chē)系統(tǒng)

      分析:

      購(gòu)物車(chē)提供的四個(gè)商品和購(gòu)買(mǎi)的數(shù)量在后臺(tái)需要容器存儲(chǔ)。

      每個(gè)商品對(duì)象都一一對(duì)應(yīng)一個(gè)購(gòu)買(mǎi)數(shù)量。

      把商品對(duì)象看成是Map集合的建,購(gòu)買(mǎi)數(shù)量看成Map集合的值。

      例如: {商品1=2 , 商品2=3 , 商品3 = 2 , 商品4= 3}

      Java中Map集合體系的基本使用和常用API

      Map集合體系的特點(diǎn)

      Java中Map集合體系的基本使用和常用API

      Map集合中使用最多的Map集合是HashMap。

      重點(diǎn)掌握HashMap , LinkedHashMap , TreeMap。其他的后續(xù)理解。

      Map集合體系特點(diǎn):

      Map集合的特點(diǎn)都是由鍵決定的。

      Map集合的鍵是無(wú)序,不重復(fù)的,無(wú)索引的,值不做要求(可以重復(fù))。

      Map集合后面重復(fù)的鍵對(duì)應(yīng)的值會(huì)覆蓋前面重復(fù)鍵的值。

      Map集合的鍵值對(duì)都可以為null。

      Map集合實(shí)現(xiàn)類(lèi)特點(diǎn):

      HashMap:元素按照鍵是無(wú)序,不重復(fù),無(wú)索引,值不做要求。(與Map體系一致)

      public static void main(String[] args) {     // 創(chuàng)建一個(gè)HashMap對(duì)象     Map<String, Integer> maps = new HashMap<>();     // 向集合添加元素     maps.put("桌子", 2);     maps.put("凳子", 10);     maps.put("桌子", 10); // 鍵一樣會(huì)覆蓋前面的     maps.put(null, null); // 鍵值對(duì)可以為null  		// 輸出集合, 可以發(fā)現(xiàn)是無(wú)序的     System.out.println(maps); // {null=null, 凳子=10, 桌子=10}}
      登錄后復(fù)制

      LinkedHashMap:元素按照鍵是有序,不重復(fù),無(wú)索引,值不做要求。

      public static void main(String[] args) {     // 創(chuàng)建一個(gè)LinkedHashMap對(duì)象     // Map<String, Integer> maps = new HashMap<>();      Map<String, Integer> maps = new LinkedHashMap<>();     // 向集合添加元素     maps.put("桌子", 2);     maps.put("凳子", 10);     maps.put("桌子", 10); // 鍵一樣會(huì)覆蓋前面的     maps.put(null, null); // 鍵值對(duì)可以為null      // 輸出集合, 是有序的     System.out.println(maps); // {桌子=10, 凳子=10, null=null}}
      登錄后復(fù)制

      TreeMap:元素是按照鍵排序,不重復(fù),無(wú)索引的,值不做要求。

      public static void main(String[] args) {     // 創(chuàng)建一個(gè)HashMap對(duì)象     // Map<String, Integer> maps = new HashMap<>();     // Map<String, Integer> maps = new LinkedHashMap<>();     Map<String, Integer> maps = new TreeMap<>();     // 向集合添加元素     maps.put("ddd", 2);     maps.put("bbb", 10);     maps.put("ddd", 3);     maps.put("aaa", 5);     maps.put("ccc", 1);      // 輸出集合, 元素按照鍵進(jìn)行排序     System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3}}
      登錄后復(fù)制

      Map集合常用的API

      Map集合:

      Map是雙列集合的祖宗接口,它的功能是全部雙列集合都可以繼承使用的。

      Map API如下:

      方法名稱(chēng) 說(shuō)明
      put(K key,V value) 添加元素
      remove(Object key) 根據(jù)鍵, 刪除鍵值對(duì)元素
      clear() 移除所有的鍵值對(duì)元素
      containsKey(Object key) 判斷集合是否包含指定的鍵
      containsValue(Object value) 判斷集合是否包含指定的值
      isEmpty() 判斷集合是否為空
      size() 集合的長(zhǎng)度,也就是集合中鍵值對(duì)的個(gè)數(shù)

      put方法添加元素

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        System.out.println(maps); // {iPhone=6, 生活用品=15, 華為=10, 小米=5}}
      登錄后復(fù)制

      remove方法, 根據(jù)鍵刪除元素

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 刪除元素     maps.remove("小米");      System.out.println(maps); // {iPhone=6, 生活用品=15, 華為=10}}
      登錄后復(fù)制

      clear方法, 清空集合元素

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 清空元素     maps.clear();      System.out.println(maps); // {}}
      登錄后復(fù)制

      containsKey()方法, 判斷是否包含指定鍵

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 判斷是否包含指定鍵     System.out.println(maps.containsKey("華為")); // true     System.out.println(maps.containsKey("魅族")); // false}
      登錄后復(fù)制

      containsValue方法, 判斷是否包含指定值

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 判斷是否包含指定值     System.out.println(maps.containsValue(6)); // true     System.out.println(maps.containsValue(99)); // false}
      登錄后復(fù)制

      isEmpty, 判斷集合是否為空

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 判斷集合是否為空     System.out.println(maps.isEmpty()); // false}
      登錄后復(fù)制

      size方法, 集合元素的個(gè)數(shù)

      public static void main(String[] args) {     // 創(chuàng)建Map集合對(duì)象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("華為", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 返回集合元素的個(gè)數(shù)     System.out.println(maps.size()); // 4}
      登錄后復(fù)制

      擴(kuò)展方法: putAll合并其他集合, 合并遇到重復(fù)的key會(huì)進(jìn)行合并

      public static void main(String[] args) {     Map<String, Integer> map1 = new HashMap<>();     map1.put("java", 1);     map1.put("C語(yǔ)言", 2);     Map<String, Integer> map2 = new HashMap<>();     map2.put("python", 4);     map2.put("linux", 7);      // 合并兩個(gè)集合     map1.putAll(map2);     System.out.println(map1); // {{python=4, java=7, C語(yǔ)言=2}}
      登錄后復(fù)制

      推薦學(xué)習(xí):《java視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)