教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java中map的循环遍历和map的获取值的办法

java中map的循环遍历和map的获取值的办法

发布时间:2016-10-23   编辑:jiaochengji.com
教程集为您提供java中map的循环遍历和map的获取值的办法等资源,欢迎您收藏本站,我们将为您提供最新的java中map的循环遍历和map的获取值的办法资源
Java中Map的遍历方式了,这个其实有点像php中数组了,下文整理了一些map的循环遍历和map的获取值的办法,希望能帮助到各位。

map的循环遍历方式 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8662')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8662>package com.sec.map;
   
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
   
public class TestMap {
   
    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("www.jiaochengji.com");
 
          //第二种
 
          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("www.jiaochengji.com");
 
          //第三种:推荐,尤其是容量大时
 
          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("www.jiaochengji.com");
 
          //第四种
 
          System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
 
          for (String v : map.values()) {
 
           System.out.println("value= " v);
 
          }
 
          System.out.println("www.jiaochengji.com");
         }
 
}

补充:

第一种用for循环
Java代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2400')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2400>

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代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5903')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5903>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代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5337')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5337>

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代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4822')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4822>

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

您可能感兴趣的文章:
Go range实现原理及性能优化剖析
golang for range原理(转载)
基于jquery循环map功能的代码
Golang从入门到放弃200618--Map(1)Map的初始化和基本操作
golang key map 所有_golang系列——高级语法之map
golang遍历时修改被遍历对象
java中map的循环遍历和map的获取值的办法
java中关于Map的几大问题总结
golang 并发访问map遇到的问题
golang map中结构体元素是无法取地址的

[关闭]
~ ~