zoukankan      html  css  js  c++  java
  • java中遍历MAP的几种方法

    java中遍历MAP的几种方法 
    Java代码 
    Map<String,String> map=new HashMap<String,String>();    
    map.put("username", "qq");    
    map.put("passWord", "123");    
    map.put("userID", "1");    
    map.put("email", "qq@qq.com");   
    Map<String,String> map=new HashMap<String,String>(); 
    map.put("username", "qq"); 
    map.put("passWord", "123"); 
    map.put("userID", "1"); 
    map.put("email", "qq@qq.com"); 
    第一种用for循环 
    Java代码 

    for(Map.Entry<String, String> entry:map.entrySet()){    
         System.out.println(entry.getKey()+"--->"+entry.getValue());    
    }   
    for(Map.Entry<String, String> entry:map.entrySet()){ 
              System.out.println(entry.getKey()+"--->"+entry.getValue()); 


    第二种用迭代 
    Java代码 

    Set set = map.entrySet();         
    Iterator i = set.iterator();         
    while(i.hasNext()){      
         Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();    
         System.out.println(entry1.getKey()+"=="+entry1.getValue());    
    }   
    Set set = map.entrySet();     
    Iterator i = set.iterator();     
    while(i.hasNext()){  
        Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next(); 
        System.out.println(entry1.getKey()+"=="+entry1.getValue()); 

    用keySet()迭代 
    Java代码 

    Iterator it=map.keySet().iterator();    
    while(it.hasNext()){    
         String key;    
         String value;    
         key=it.next().toString();    
         value=map.get(key);    
         System.out.println(key+"--"+value);    
    }   
    Iterator it=map.keySet().iterator(); 
    while(it.hasNext()){ 
        String key; 
        String value; 
        key=it.next().toString(); 
        value=map.get(key); 
        System.out.println(key+"--"+value); 


    用entrySet()迭代 
    Java代码 

    Iterator it=map.entrySet().iterator();           
    System.out.println( map.entrySet().size());    
    String key;           
    String value;    
    while(it.hasNext()){    
            Map.Entry entry = (Map.Entry)it.next();           
            key=entry.getKey().toString();           
            value=entry.getValue().toString();           
            System.out.println(key+"===="+value);                     
    }   

  • 相关阅读:
    蝶恋花
    JVM解毒——JVM与Java体系结构
    超赞!IDEA 最新版本,支持免打扰和轻量模式!
    SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存
    神奇的 SQL 之 WHERE 条件的提取与应用
    终于放弃了单调的swagger-ui了,选择了这款神器—knife4j
    Git 高级用法,喜欢就拿去用
    既然有 HTTP 请求,为什么还要用 RPC 调用?
    SpringBoot和Spring到底有没有本质的不同?
    一条简单的更新语句,MySQL是如何加锁的?
  • 原文地址:https://www.cnblogs.com/ChrisMurphy/p/5005944.html
Copyright © 2011-2022 走看看