java map使用

來源:互聯網
上載者:User

標籤:import   map   imp   pre   ack   set   ext   print   port   

一:

map,他是有雙列的集合組成,即:key=value形式。

 1 package test12; 2  3 import java.util.*; 4  5 public class Map_Demo { 6     public static  void  main(String...args){ 7         map_Demo(); 8     } 9     public static  void  map_Demo(){10         Map<String,Integer> per_info= new HashMap<>();11         per_info.put("tom",22);//設定key val12         per_info.put("tom",22);13         per_info.put("ok",22);14         Set<String> key=per_info.keySet();//擷取key的集合.因為key無序.15         Iterator<String> k_it=key.iterator();16         while (k_it.hasNext()){17             System.out.print(k_it.next());18         }19     }20 }

 其中:

v       put(k,v)  插入key、val。map方法雖然要求key不能重複,但是如果插入的key是重複的,也不會報錯。

Set   keyset()擷取map的key集合,注意類型為set。

 其中put方法的傳回值為v即被覆蓋的value值。只有當覆蓋相同的key值的情況下。如果新插入的值為null。

 1 package test12; 2  3 import java.util.*; 4  5 public class Map_Demo { 6     public static  void  main(String...args){ 7         map_Demo(); 8     } 9     public static  void  map_Demo(){10         Map<String,Integer> per_info= new HashMap<>();11         Integer i=per_info.put("tom",22);//設定key val12         Integer j=per_info.put("tom",22);13         per_info.put("ok",22);14         System.out.print(i);15         System.out.print(j);16         Set<String> key=per_info.keySet();//擷取key的集合.因為key無序.17         Iterator<String> k_it=key.iterator();18         while (k_it.hasNext()){19             System.out.print(k_it.next());20         }21     }22 }

 

 

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.