[java]源码详解三种map遍历方式
java程序员会经常接触到集合类容器,今天,总结一下map的遍历方式,目的很简单,能自如的操作map即可,看源码:[java]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. 补充:软件开发 , Java ,
上一个:设计模式之工厂模式
下一个:排序算法之直接插入排序(JAVA)
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值