【Java學習筆記】Map集合的keySet,entrySet,values的用法例子,keysetentryset

來源:互聯網
上載者:User

【Java學習筆記】Map集合的keySet,entrySet,values的用法例子,keysetentryset

 1 import java.util.Collection; 2 import java.util.HashMap; 3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.Set; 6  7 public class MapDemo { 8  9     public static void main(String[] args) {10         Map<Integer,String> map = new HashMap<Integer,String>();11 12         13         map.put(8, "zhaoliu");14         map.put(9, "zhaoliu");15         map.put(1, "xiaoqiang");16         map.put(6, "wangcai");17         map.put(7, "zhaoliu");18         map.put(99, "zhaoliu");19         map.put(87, "xiaoqiang");20         map.put(42, "wangcai");21         22         show1(map);23         24         show2(map);25         26         showValue(map);27 28     }29 30     public static void showValue(Map<Integer, String> map) {31         Collection<String> values = map.values();32         Iterator<String> it = values.iterator();33         while (it.hasNext())34         {35             System.out.println(it.next());36         }37     }38 39     public static void show2(Map<Integer, String> map) {40         //通過Map轉成Set就可以迭代41         //找到了另一種方法。  entrySet42         //該方法將鍵和值的映射關係作為Object Storage Service到了Set集合中,而這個映射關係的類型就是Map.Entry類型(結婚證)43         44         Set<Map.Entry<Integer, String>> entrySet = map.entrySet();45         Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();46         /*47          *也可以寫成 Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();48          */49         50         while(it.hasNext())51         {52             Map.Entry<Integer, String> me = it.next();53             Integer key = me.getKey();54             String value = me.getValue();55             System.out.println(key+"::"+value);56             57         }58     }59 60     public static void show1(Map<Integer, String> map) {61         //取出map中的所有元素。62         //原理:通過keySet方法擷取map中所有的鍵所在的Set集合,在通過Set的迭代器擷取到每一個鍵,63         //在對每一個鍵通過map集合的get方法擷取其對應的值即可。64         65         Iterator<Integer> it = map.keySet().iterator();66         /*67          * 相當於  Set<Integer> keySet = map.keySet();68          *      Iterator<Integer> it = keySet.iterator();69          *70          */71         72         while (it.hasNext())73         {74             Integer key = it.next();75             String value = map.get(key);76             System.out.println(key+"="+value);77         }78     }79 80 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.