Map
2016-04-25 14:04
232 查看
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class TraversalMap { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("username", "qq"); map.put("password", "123"); map.put("userID", "1"); map.put("email", "qq@qq.com"); forMap(map); System.out.println("--------------"); iteratorMap(map); System.out.println("--------------"); keySetMap(map); System.out.println("--------------"); entrySetMap(map); } //entrySet() private static void entrySetMap(Map<String, String> map) { 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); } } //keySet() private static void keySetMap(Map<String, String> map) { 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 private static void iteratorMap(Map<String, String> map) { Set set = map.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) i .next(); System.out.println(entry.getKey() + "==" + entry.getValue()); } } //for private static void forMap(Map<String, String> map) { for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "--->" + entry.getValue()); } } }
相关文章推荐
- 两个小数相加
- Linux性能监控工具dstat
- 对“视觉机器学习20讲配套仿真代码”的研究心得---贝叶斯学习
- Linux下git使用详解1
- 二、 sql*plus常用命令
- 使用hexo搭建属于自己的博客
- android监听通讯录内容变化
- 如何从命令行实时监控Nginx Web服务器
- 51nod 1076 2条不相交的路径(tarjan)
- Found 2 versions of android-support-v4.jar in the dependency list,but not all the versions are ident
- bean validation 分组验证及分组顺序---学习笔记
- 剑指offer(15):调整数组顺序使奇数位于偶数前面
- Hive的三种调用方式
- Java的多态在方法重载和重写时的情况
- 从前端开始
- sublime text 3103 怎么设置中文
- HTML学习笔记
- 记录遇到的IE8兼容性问题汇总
- 转自小木虫:三年来评审18篇SCI文章的一点感受
- qt的addWidget、setColumnStretch 等的使用方法