遍历Map
Java中的map类型如何遍历例如:
HashMap<String, Object>map=new HashMap<String, Object>();
for(int i=0;i<15;i++){
map.put("key_x"+i,"你好"+i);
}
//如何遍历map。。。 --------------------编程问答-------------------- Collection<Object> c = map.values();
Iterator<Object> l = c.iterator();
while (l.hasNext()) {
System.out.println(l.next());
} --------------------编程问答--------------------
//通过key来遍历
Map m = new HashMap();
Set set = m.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
m.get(i.next()) ;
}
--------------------编程问答--------------------
--------------------编程问答-------------------- map不能遍历,Map有个entrySet映射
Map m = new HashMap();
for(Object o : map.keySet()){
map.get(o);
}
API上是这么说的
返回Set<Map.Entry<K,V>>
entrySet()返回此映射所包含的映射关系的 collection 视图。
这么写应该就行了
Set<Map.Entry<String, Object>> mapEntrySet = map.entrySet();
for(int i=0;i<mapEntrySet.size();i++){
......
} --------------------编程问答-------------------- 纠正下上面的,遍历set要用iterator,不能用for --------------------编程问答-------------------- HashMap<String, Object>map=new HashMap<String, Object>();
for(int i=0;i<15;i++){
map.put("key_x"+i,"你好"+i);
}
//通过key来遍历
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
System.out.println("**"+map.get(i.next())+"******");
}
2楼,这样执行后的结果是:**null******
**null******
**null******
**null******
。。。
你刚才试过没有,在看看
--------------------编程问答--------------------
--------------------编程问答--------------------
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
--------------------编程问答-------------------- 使用增强for嘛
第一种:效率高
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
第二种:效率低
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
for(Object o:map){
system.out.print(o.getKey);
system.out.println(o.getValue());
} --------------------编程问答-------------------- 我还是喜欢用foreach:
--------------------编程问答-------------------- Map的三种遍历方法
for (String str : map.keySet()) {
System.out.println(str + " : " + map.get(str));
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
import java.util.*;
public class MapTraverse {
public static void main(String[] args) {
String[] str = {"I love you", "You love he", "He love her", "She love me"};
Map<Integer, String> m = new HashMap();
for(int i=0; i<str.length; i++) {
m.put(i, str[i]);
}
System.out.println("下面是使用useWhileSentence()方法输出的结果:");
useWhileSentence(m);
System.out.println("下面是使用useWhileSentence2()方法输出的结果:");
useWhileSentence2(m);
System.out.println("下面是使用useForSentence()方法输出的结果:");
useForSentence(m);
}
public static void useWhileSentence(Map<Integer, String> m) {
Set s = (Set<Integer>)m.keySet();
Iterator<Integer> it = s.iterator();
int Key;
String value;
while(it.hasNext()) {
Key = it.next();
value = (String)m.get(Key);
System.out.println(Key+":\t"+value);
}
}
public static void useWhileSentence2(Map m) {
Set s = m.entrySet();
Iterator<Map.Entry<Integer, String>> it = s.iterator();
Map.Entry<Integer, String> entry;
int Key;
String value;
while(it.hasNext()) {
entry = it.next();
Key = entry.getKey();
value = entry.getValue();
System.out.println(Key+":\t"+value);
}
}
public static void useForSentence(Map<Integer, String> m) {
int Key;
String value;
for(Map.Entry<Integer, String> entry : m.entrySet()) {
Key = entry.getKey();
value = entry.getValue();
System.out.println(Key+":\t"+value);
}
}
}
http://blog.csdn.net/tsyj810883979/article/details/6746274 --------------------编程问答-------------------- 楼主,8楼说的很好,map就这两种遍历方式。会用就好!!
补充:Java , Java EE