zoukankan      html  css  js  c++  java
  • TestNG entryset的用法及遍历map的用法

    以下内容引自 http://blog.csdn.net/bestone0213/article/details/47904107  (注: 该 url不是原出处。其博主注明转载,但未注明转自何处)

    keySet是键的集合,Set里面的类型即key的类型
    entrySet是 键-值 对的集合,Set里面的类型是Map.Entry
     

    1.keySet()

    Map map=new HashMap();

    Iterator it=map.keySet().iterator();

    Object key;

    Object value;

    while(it.hasNext()){

    key=it.next();

    value=map.get(key);

    System.out.println(key+":"+value);

    }

    2.entrySet()

    Map map=new HashMap();

    Iterator it=map.entrySet().iterator();

    Object key;

    Object value;

    while(it.hasNext()){

    Map.Entry entry = (Map.Entry)it.next();

    key=entry.getKey();

    value=entry.getValue();

    System.out.println(key+"="+value);

    }

     

    public static void main(String[] args) {


      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //第二种
      System.out.println("通过Map.entrySet使用iterator遍历key和value:");
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      
      //第三种:推荐,尤其是容量大时
      System.out.println("通过Map.entrySet遍历key和value");
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }

      //第四种
      System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }

  • 相关阅读:
    对文件下载的补充
    IBatisNet1.5学习配置篇
    IBatisnet Facility 的几种配置
    ERP术语 英文对照(部分)(参考)
    使用IBatisNet + Castle 开发DotNet软件
    JS屏蔽浏览器右键菜单
    恢复误删数据(SQL Server 2000)--Log Explorer
    IBatisNet1.5 映射文件Parameter Maps and Inline Parameters
    深圳电话订票基本步骤及所有的取票点地址电话
    DataFormatString格式化字符串
  • 原文地址:https://www.cnblogs.com/cheese320/p/8311514.html
Copyright © 2011-2022 走看看