map的三种遍历方法!
来源:互联网 发布:aws 阿里云 编辑:程序博客网 时间:2024/06/05 12:49
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.huitu.test;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;/** * * @author Administrator */public class TestMap { public static void main(String[] args) { Map<String, Student> map = new HashMap<String, Student>(); Student s1 = new Student("宋江", "1001", 38); Student s2 = new Student("卢俊义", "1002", 35); Student s3 = new Student("吴用", "1003", 34); map.put("1001", s1); map.put("1002", s2); map.put("1003", s3); Map<String, Student> subMap = new HashMap<String, Student>(); subMap.put("1008", new Student("tom", "1008", 12)); subMap.put("1009", new Student("jerry", "1009", 10)); map.putAll(subMap); work(map); workByKeySet(map); workByEntry(map); } //最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!! public static void work(Map<String, Student> map) { Collection<Student> c = map.values(); Iterator it = c.iterator(); for (; it.hasNext();) { System.out.println(it.next()); } }//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!! public static void workByKeySet(Map<String, Student> map) { Set<String> key = map.keySet(); for (Iterator it = key.iterator(); it.hasNext();) { String s = (String) it.next(); System.out.println(map.get(s)); } }//比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~ public static void workByEntry(Map<String, Student> map) { Set<Map.Entry<String, Student>> set = map.entrySet(); for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) { Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next(); System.out.println(entry.getKey() + "--->" + entry.getValue()); } }}class Student { private String name; private String id; private int age; public Student(String name, String id, int age) { this.name = name; this.id = id; this.age = age; } @Override public String toString() { return "Student{" + "name=" + name + "id=" + id + "age=" + age + '}'; }}
参考资料:http://www.cnblogs.com/christ/archive/2010/10/30/1865134.html
- 遍历的Map的三种方法
- Map的三种遍历方法
- map的三种遍历方法!
- Map遍历的三种方法
- map的三种遍历方法!
- java map的三种遍历方法!
- map的三种遍历方法!
- map的三种遍历方法
- map的三种遍历方法
- map的三种遍历方法!
- map的三种遍历方法!
- Java.Map的三种遍历方法
- 遍历Map集合的三种方法
- map的三种遍历方法!
- map的三种遍历方法!
- java遍历Map的三种方法
- map的三种遍历方法!
- map的三种遍历方法!
- JavaScript的self和this使用小结
- Internal table to PDF
- LA4394
- AllJoyn:近距离P2P通讯技术 1Km啊 好东西
- c++ multiset
- map的三种遍历方法!
- SDL源码阅读笔记(2) video dirver的初始化及选择
- 类网易新闻首页ListView---异步加载ListView图片
- 网页设计实训项目八——框架布局
- 折半插入排序
- Java、Android超精确测量代码执行时间差
- 安卓自动化测试工具MonkeyRunner之使用ID
- SDL源码阅读笔记(3)渲染模块
- PHP大型网站的核心技术之-memcached视频教程