java 中 Map遍历
来源:互联网 发布:c语言猴子吃桃递归 编辑:程序博客网 时间:2024/04/29 05:24
第二种方法效率更高
法①
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
例子:
package com.cric.cat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapTest1 {
public static void main(String[] args) {
Student stu1=new Student("告诫去", "农大");
Student stu2 = new Student("往基隆", "海大");
Map M = new HashMap();
M.put("jay",stu1);
M.put("wang", stu2);
Iterator it=M.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println(key);
System.out.println(value);
}
}
}
package com.cric.cat;
public class Student {
private String name;
private String school;
public Student(String name, String school) {
this.name = name;
this.school = school;
}
public String toString() {
return school + "毕业的" + name;
}
}
法②
HashMap<String, String> map = new HashMap<String, String>();
map.put("jay", stu1.toString());
map.put("wang", stu2.toString());
for (Entry b : map.entrySet()) {
System.out.println(b.getKey());
System.out.println(b.getValue());
例子:
package com.cric.cat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class MapTest {
public static void main(String[] args) {
Student stu1 = new Student("告诫去", "农大");
Student stu2 = new Student("往基隆", "海大");
HashMap<String, String> map = new HashMap<String, String>();
map.put("jay", stu1.toString());
map.put("wang", stu2.toString());
for (Entry b : map.entrySet()) {
System.out.println(b.getKey());
System.out.println(b.getValue());
}
}
}
package com.cric.cat;
public class Student {
private String name;
private String school;
public Student(String name, String school) {
this.name = name;
this.school = school;
}
public String toString() {
return school + "毕业的" + name;
}
}
- java 中 Map遍历
- java 中 Map遍历
- java中遍历Map
- java中遍历map
- Java中遍历Map
- Java 中 Map 的遍历
- java中遍历一个Map
- Java中Map的遍历
- java中遍历MAP方法
- java中Map的遍历
- JAVA中如何遍历Map
- java中 Map 遍历方法
- java中entry遍历map
- java中遍历一个Map
- java中Map的遍历
- Java中遍历Map对象
- Java中Map的遍历
- Java中Map的遍历
- Windows XP、 vista 、WIn7系统性能横向测评
- Proxool连接池的使用心得
- DVD区域码相关知识
- ADC &TOUCH
- ASP .NET 中 AJAX 的 TabContainer 的 CSS 样式编写
- java 中 Map遍历
- lucene-sandbox工具包
- string类应用方法
- dwr 统计网站访问量
- 遍历HashMap
- word删除超链接的方法
- 遍历哈希表
- 去除网页中代码的行号
- lucene-创建文档索引处理框架