欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

遍历Map的几种方法

程序员文章站 2022-04-08 14:00:22
...
		Map<String,String> map=new HashMap<String,String>();
		map.put("username", "qq");
		map.put("passWord", "123");
		map.put("userID", "1");
		map.put("email", "[email protected]");

 第一种用for循环

		for(Map.Entry<String, String> entry:map.entrySet())
		{
			System.out.println(entry.getKey()+"--->"+entry.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()迭代

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()迭代

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

 

相关标签: QQ