• 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);                     
    }   

    上述是转载网址: http://rain-2372.iteye.com/blog/1615615

    下述代码是自己增加:

    System.out.println("==================================================");

    // 定义Map 
    Map<String, Object> mapTestValue1 = new HashMap<String , Object>();
    Map<String, Object> mapTestValue2 = new HashMap<String , Object>();
    Map<String, Object> mapTestValue3 = new HashMap<String , Object>();

    mapTestValue1.put("111", "1111");
    mapTestValue2.put("222", "2222");
    mapTestValue3.put("333", "3333");

    // 定义Map 嵌套Map<String , Object> 对象

    // 第一种定义方式
    Map<String , Map<String, Object>> mapObject = FastMap.newInstance();
    // 第二种定义方式
    Map<String , Map<String, Object>> mapObject2 = new HashMap<String, Map<String, Object>>() ;

    // 将Map 作为Value 加载到Map对象中
    mapObject.put("11", mapTestValue1);
    mapObject.put("22", mapTestValue2);
    mapObject.put("33", mapTestValue3);

    // 遍历map数据 使用Set格式进行接收
    Set<Entry<String, Map<String, Object>>> setMap = mapObject.entrySet();

    for (Entry<String , Map<String , Object>> setEntry : setMap) {
    String key = setEntry.getKey() ;
    System.out.println("setEntry.getKey() : " + setEntry.getKey() + " , setEntry.getValue() : " + setEntry.getValue() );

    Map<String , Object> mapKeyValue = setEntry.getValue() ;
    // System.out.println("mapKeyValue.getKey() : " + mapKeyValue. + " , mapKeyValue.getValue() : " + mapKeyValue.getValue() );
    System.out.println("111 : " + mapKeyValue.get("111"));
    System.out.println("222 : " + mapKeyValue.get("222"));
    System.out.println("333 : " + mapKeyValue.get("333"));
    }

  • 相关阅读:
    Python解释器安装
    有钱就放余额宝的人,这个习惯恐怕要改一改!
    这么详细的存储基础知识,你不看看? 会后悔的!
    超全!华为交换机端口vlan详解~
    华为:鸿蒙绝不是安卓换皮!!!
    VS Code 真的会一统江湖吗?
    用户与安全 -(1)Linux用户及组管理
    运维必看!这个技能薪水28.8万,工资竟然还只是零花钱....
    原来 Linux 日志文件系统是这样工作的~
    干货长文:Linux 文件系统与持久性内存介绍
  • 原文地址:https://www.cnblogs.com/meimao5211/p/6197985.html
Copyright © 2020-2023  润新知