【java】源码详解三种map遍历方式

java程序员会经常接触到集合类容器,今天,总结一下map的遍历方式,目的很简单,能自如的操作map即可,看源码:

package dec;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 
 * 
 * <p>
 * Title: map遍历测试类 /p>
 * 
 * <p>
 * Description: 示例 业务类
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * 
 * @author dml@2012-12-25
 * @version 1.0
 */

public class MapIterator {

	public static void main(String[] args) {
		String[] str = { "I love you", "You love him", "He loves her",
				"She loves me" };
		Map<Integer, String> m = new HashMap();
		for (int i = 0; i < str.length; i++) {
			m.put(i, str[i]);
		}
		System.out.println("下面是使用useKeySet()方法输出的结果:");
		useKeySet(m);
		System.out.println("下面是使用useEntrySet()方法输出的结果:");
		useEntrySet(m);
		System.out.println("下面是使用useValues()方法输出的结果:");
		useValues(m);
	}

	/**
	 * 1. 使用keySet遍历
	 * 
	 * Set java.util.Map.keySet()
	 * 
	 * Returns a Set view of the keys contained in this map. The set is backed
	 * by the map, so changes to the map are reflected in the set, and
	 * vice-versa. If the map is modified while an iteration over the set is in
	 * progress (except through the iterator's own remove operation), the
	 * results of the iteration are undefined. The set supports element removal,
	 * which removes the corresponding mapping from the map, via the
	 * Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
	 * It does not support the add or addAll operations.
	 * 
	 * Returns: a set view of the keys contained in this map
	 * 
	 * @param m
	 */
	public static void useKeySet(Map m) {
		Set s = m.keySet();
		Iterator it = s.iterator();
		int Key;
		String value;
		while (it.hasNext()) {
			Key = (Integer) it.next();
			value = (String) m.get(Key);
			System.out.println(Key + ":\t" + value);
		}
	}

	/**
	 * 2.使用entrySet遍历
	 * 
	 * Set java.util.Map.entrySet()
	 * 
	 * Returns a Set view of the mappings contained in this map. The set is
	 * backed by the map, so changes to the map are reflected in the set, and
	 * vice-versa. If the map is modified while an iteration over the set is in
	 * progress (except through the iterator's own remove operation, or through
	 * the setValue operation on a map entry returned by the iterator) the
	 * results of the iteration are undefined. The set supports element removal,
	 * which removes the corresponding mapping from the map, via the
	 * Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
	 * It does not support the add or addAll operations.
	 * 
	 * Returns: a set view of the mappings contained in this map
	 * 
	 * @param m
	 */
	public static void useEntrySet(Map m) {
		Set s = m.entrySet();
		Iterator it = s.iterator();
		Map.Entry entry;
		int Key;
		String value;
		while (it.hasNext()) {
			entry = (Entry) it.next();
			System.out.println(entry.getKey() + ":\t" + entry.getValue());
		}
	}

	/**
	 * 3.使用values遍历
	 * 
	 * Collection<String> java.util.Map.values()
	 * 
	 * 
	 * Returns a Collection view of the values contained in this map. The
	 * collection is backed by the map, so changes to the map are reflected in
	 * the collection, and vice-versa. If the map is modified while an iteration
	 * over the collection is in progress (except through the iterator's own
	 * remove operation), the results of the iteration are undefined. The
	 * collection supports element removal, which removes the corresponding
	 * mapping from the map, via the Iterator.remove, Collection.remove,
	 * removeAll, retainAll and clear operations. It does not support the add or
	 * addAll operations.
	 * 
	 * Returns: a collection view of the values contained in this map
	 * 
	 * @param m
	 */
	public static void useValues(Map<Integer, String> m) {
		Collection c = m.values();
		Iterator it = c.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}

	}

}

执行结果:

下面是使用useKeySet()方法输出的结果:
0:	I love you
1:	You love him
2:	He loves her
3:	She loves me
下面是使用useEntrySet()方法输出的结果:
0:	I love you
1:	You love him
2:	He loves her
3:	She loves me
下面是使用useValues()方法输出的结果:
I love you
You love him
He loves her
She loves me



dml@2012.12.25
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值