//用HashMap存储字符串并遍历
////由键找值
import java.util.Collection;
import java.util.HashMap;
public class MyHashMap {
public static void main(String[] args) {
HashMap<String,String> map=new HashMap<String,String>( );
map.put("it001", "zhangsan");
map.put("it002", "lisi");
map.put("it003", "wangwu");
//由键找值
//定义集合接收键
Collection<String> set=map.keySet();
//增强for遍历集合
for(String key:set){
String value=map.get(key);
System.out.println(key+"\t"+value);
}
}
}
//将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry
import java.util.Collection;
import java.util.HashMap;
public class MyHashMap {
public static void main(String[] args) {
HashMap<String,String> map=new HashMap<String,String>();
map.put("it001", "zhangsan");
map.put("it002", "lisi");
map.put("it003", "wangwu");
Set<Map.Entry<String,String>> set=map.entrySet();
for(Map.Entry<String,String> me:set){
String key=me.getKey();
String value=me.getValue();
System.out.println(key+"\t"+value);
}
}
} |