<p id="g32nn"></p>
    1. <acronym id="g32nn"><strong id="g32nn"></strong></acronym>
      <pre id="g32nn"></pre>

      <table id="g32nn"><option id="g32nn"></option></table>

          Java集合框架--Map接口
          2022-09-06 22:37:32

          ?

          Map接口:

            1、采用鍵值對的形式存儲對象

            2、Key不能重復,value可以重復

            3、主要實現類:HashMap?? TreeMap??? Hashtable

          ?

          HashMap:

            public class HashMap<K,V> extends AbstractMap<K,V>

            implements Map<K,V> ,Cloneable,Serializable

            基于哈希表實現,允許key、value為 null,除了非同步和允許Null外其他的

            和Hashtable相似,此類不保證映射的順序,也不保證順序恒久不變。

          代碼示例

          package com.collection.map;
          
          import java.util.Collection;
          import java.util.HashMap;
          import java.util.Map;
          import java.util.Set;
          
          public class MapDemo {
              public static void main(String[] args) {
                  hashMap();
              }
          
          
              public static void hashMap(){
                  Map<Integer,String> hm = new HashMap<>();
                  hm.put(1,"AA");
                  hm.put(2,"BB");
                  hm.put(3,"CC");
                  hm.put(4,"DD");
          
                  //根據 Entry<K,V> 遍歷
                  Set<Map.Entry<Integer,String>> entry= hm.entrySet();
                  for (Map.Entry en:entry){
                      System.out.println(en.getKey()+":"+en.getValue());
                  }
          
                  System.out.println("*************************");
                  //根據Keyset遍歷
                  Set<Integer> keyset=hm.keySet();
                  for(Integer i:keyset){
                      System.out.println(i+":"+hm.get(i));
                  }
          
                  //根據valueSet遍歷
                  System.out.println("*************************");
                  Collection<String> co= hm.values();
                  for(String s:co){
                      System.out.println(s);
                  }
          
                  System.out.println("*************************");
                  //JDK1.8新的foreach
                  hm.forEach((k,v)->{System.out.println(k+":"+v);});
              }
          }

          ?

          ?

            

          ?

          ?

          ?

            

          本文摘自 :https://www.cnblogs.com/


          更多科技新聞 ......

          97久久久久人妻精品专区_国产成人精品视频导航_国产色诱视频在线播放网站_97午夜理论电影影院
          <p id="g32nn"></p>
          1. <acronym id="g32nn"><strong id="g32nn"></strong></acronym>
            <pre id="g32nn"></pre>

            <table id="g32nn"><option id="g32nn"></option></table>