HashMap集合遍历的四种方式

对于Map来说,遍历的方式都是一样的,大概都是有四种形式

  1. 直接遍历
  2. 返回keySet()
  3. 返回Values()
  4. 返回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);

		// 第一种遍历方式 : jdk1.8 新特性
		map.forEach((k, v) -> System.out.println(k+"..."+v));

        // 第二种遍历方式 : 通过返回键集
        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();

	}
}

你可能感兴趣的:(#,Java基础知识)