JAVA循环遍历Map的几种方法
1个回答
展开全部
第一种用for循环
Java代码
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代
Java代码
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
用keySet()迭代
Java代码
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);
}
用entrySet()迭代
Java代码
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);
}
Java代码
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
第二种用迭代
Java代码
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();
System.out.println(entry1.getKey()+"=="+entry1.getValue());
}
用keySet()迭代
Java代码
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);
}
用entrySet()迭代
Java代码
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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询