对于Map来说,遍历的方式都是一样的,大概都是有四种形式
直接遍历
返回keySet()
返回Values()
返回entrySet()
对于第四种方式可能会除了返回的可以直接打印外,还可以通过返回Map.Entry类来依次遍历该集合返回key和value值
import java.util.*;
public class HashMapDemo {
    public static void main(String[] args) {
        Map hm = new HashMap<>();
        hm.put("姓名", "Jack");
        hm.put("age", 18);
        hm.put("身高", '?');
        hm.put("身高", 178);
        // 键和值都允许为 null
        hm.put(null, null);
        // 第一种遍历方式 : 直接输出该对象
        System.out.println("直接打印");
        System.out.println(hm);
        // 第二种遍历方式 : 通过返回键集
        System.out.println("keySet()");
        Set keySet = hm.keySet();
        Iterator iter = keySet.iterator();
        while (iter.hasNext()) {
            Object next = iter.next();
            System.out.println(next + "," + hm.get(next));
        }
        /*
 首先返回的都是集合类型,除了可以使用iterator进行遍历外,还可以使用增强for循环也是非常方便的.
 其实增强for循环就是上面形式的简写.本质也是上面的形式.
 */
        // 增强for循环
        System.out.println("增强for循环");
        for (Object s :
                keySet) {
            System.out.println(s + "," + hm.get(s));
        }
        // 第三种遍历方式 : 通过返回值集
        System.out.println("value()");
        Collection values = hm.values();
        Iterator iter1 = values.iterator();
        while (iter1.hasNext()) {
            System.out.println(iter1.next());
        }
        // 第四种遍历方式 : 通过返回key-value集
        System.out.println("entrySet()集");
        Iterator iter2 = hm.entrySet().iterator();
        while (iter2.hasNext()) {
            System.out.println(iter2.next());
        }
        /*
        第四种方式的细化,将返回的key-value集分别取出
         */
        while (iter2.hasNext()) {
            Map.Entry next = (Map.Entry) iter2.next();
            System.out.println(next.getKey() + " . " + next.getValue());
        }
        /*
        第四种方式细化的简化形式.
         */
        Set<Map.Entry> entrySet = hm.entrySet();
        for (Map.Entry e : entrySet) {
            System.out.println(e.getKey() + " , " + e.getValue());
        }
    }
}
上面代码的反编译 :
import java.io.PrintStream;
import java.util.*;
public class HashMapDemo
{
 public HashMapDemo()
 {
 }
 public static void main(String args[])
 {
 Map hm = new HashMap();
 hm.put("姓名", "Jack");
 hm.put("age", Integer.valueOf(18));
 hm.put("身高", Character.valueOf('?'));
 hm.put("身高", Integer.valueOf(178));
 hm.put(null, null);
 
 System.out.println("直接打印");
 System.out.println(hm);
 
 System.out.println("keySet()");
 Set keySet = hm.keySet();
 Object next;
 for (Iterator iter = keySet.iterator(); iter.hasNext(); 
 System.out.println((new StringBuilder()).append(next).append(",").append(hm.get(next)).toString()))
 next = iter.next();
 System.out.println("增强for循环");
 Object s;
 for (Iterator iterator = keySet.iterator(); iterator.hasNext(); 
 System.out.println((new StringBuilder()).append(s).append(",").append(hm.get(s)).toString()))
 s = iterator.next();
 System.out.println("value()");
 Collection values = hm.values();
 for (Iterator iter1 = values.iterator(); iter1.hasNext(); System.out.println(iter1.next()));
 System.out.println("entrySet()集");
 Iterator iter2;
 for (iter2 = hm.entrySet().iterator(); iter2.hasNext(); System.out.println(iter2.next()));
 java.util.Map.Entry next;
 
 for (; iter2.hasNext(); 
 System.out.println((new StringBuilder()).append(next.getKey()).append(" . ").append(next.getValue()).toString()))
 next = (java.util.Map.Entry)iter2.next();
 Set entrySet = hm.entrySet();
 java.util.Map.Entry e;
 for (Iterator iterator1 = entrySet.iterator(); iterator1.hasNext(); 
 System.out.println((new StringBuilder()).append(e.getKey()).append(" , ").append(e.getValue()).toString()))
 e = (java.util.Map.Entry)iterator1.next();
 }
}
	    
	    										
					
					本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。