zoukankan      html  css  js  c++  java
  • Map的四种遍历方式- 详解以及用法

    Entry

    由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系。 
    Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也就是一个Entry)
    Map.Entry里面包含getKey()和getValue()方法

    1 Iterator<Map.Entry<Integer, Integer>> it=map.entrySet().iterator();
    2     while(it.hasNext()) {
    3         Map.Entry<Integer,Integer> entry=it.next();
    4         int key=entry.getKey();
    5         int value=entry.getValue();
    6         System.out.println(key+" "+value);
    7     }

    entrySet

    entrySet是 java中 键-值 对的集合,Set里面的类型是Map.Entry,一般可以通过map.entrySet()得到。

    • entrySet实现了Set接口,里面存放的是键值对。一个K对应一个V。

    用来遍历map的一种方法。

    1 Set<Map.Entry<String, String>> entryseSet=map.entrySet();
    2  
    3 for (Map.Entry<String, String> entry:entryseSet) {
    4  
    5     System.out.println(entry.getKey()+","+entry.getValue());
    6  
    7 }

    即通过getKey()得到K,getValue得到V。

    keySet

    还有一种是keySet, keySet是键的集合,Set里面的类型即key的类型

    1 Set<String> set = map.keySet();
    2  
    3 for (String s:set) {
    4     System.out.println(s+","+map.get(s));
    5 }

    四种遍历Map方式:

     1 public static void main(String[] args) {
     2  
     3     Map<String, String> map = new HashMap<String, String>();
     4     map.put("1", "value1");
     5     map.put("2", "value2");
     6     map.put("3", "value3");
     7   
     8     //第一种:普遍使用,二次取值
     9     System.out.println("通过Map.keySet遍历key和value:");
    10     for (String key : map.keySet()) {
    11         System.out.println("key= "+ key + " and value= " + map.get(key));
    12     }
    13   
    14     //第二种
    15     System.out.println("通过Map.entrySet使用iterator遍历key和value:");
    16     Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
    17     while (it.hasNext()) {
    18         Map.Entry<String, String> entry = it.next();
    19         System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    20     }
    21   
    22     //第三种:推荐,尤其是容量大时
    23     System.out.println("通过Map.entrySet遍历key和value");
    24     for (Map.Entry<String, String> entry : map.entrySet()) {
    25         System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    26     }
    27  
    28     //第四种
    29     System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
    30     for (String v : map.values()) {
    31         System.out.println("value= " + v);
    32     }
    33  }

    to:https://blog.csdn.net/q5706503/article/details/85122343

  • 相关阅读:
    [转] 余国藩:人文学科何以不是科学
    [openssl][nginx] 使用openssl模拟ssl/tls客户端测试nginx stream
    [openssl] 使用openssl生成证书
    [bluez] linux下蓝牙鼠标的延迟问题
    很好的一篇文章讲epoll
    [ipsec][strongswan] VirtualPN隧道网络加速FEC(forward error correction)
    [ipsec][crypto] ike/ipsec与tls的认证机制比较
    [ipsec][crypto] 有点不同的数字证书到底是什么
    [ike][ipsec] child sa rekey机制的细节分析
    [dev][nginx] 在阅读nginx代码之前都需要准备什么
  • 原文地址:https://www.cnblogs.com/shenjiangwei/p/14177680.html
Copyright © 2011-2022 走看看