普通JAVA類遍歷Map集合java
Map map=new HashMap();spa
map.put("a", "1");對象
map.put("b", "2");內存
map.put("c", "3");get
map.keySet();//鍵集合hash
map.values();//值集合it
map.entrySet();//鍵值集合class
//遍歷鍵,經過鍵取值import
Set set = map.keySet();hashmap
for (Object key : set) {
System.out.println("鍵:"+key+" 值:"+map.get(key));
}
//遍歷鍵集合
Iterator it=map.keySet().iterator();
while(it.hasNext()){
System.out.println("鍵:"+it.next());
}
//遍歷鍵值集合
Iterator it2=map.entrySet().iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
使用EL表達式遍歷Map集合
<%
Map map=new HashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="m">
鍵:${m.key}
值:${m.value}
</c:forEach>
使用EL表達式遍歷Map集合(值存放的是對象)
<%
Person p=new Person();
p.setAddress("aa");
p.setName("aa");
Person p2=new Person();
p2.setAddress("bb");
p2.setName("bb");
Person p3=new Person();
p3.setAddress("cc");
p3.setName("cc");
Map map=new HashMap();
map.put("a", p);
map.put("b", p2);
map.put("c", p3);
pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="m">
${m.key}
${m.value.name} //m.value取得的是對象的內存地址,因此能夠直接用m.value.name(name爲Person對象的屬性)
</c:forEach>
使用struts標籤<logic:iterate />遍歷
<%
List list=new ArrayList();
list.add("aa");
list.add("bb");
list.add("cc");
Map map=new HashMap();
map.put("a", list);
pageContext.setAttribute("map",map);
%>
<logic:iterate id="m" name="map">
鍵:${m.key}
<logic:iterate id="childm" name="m" property="value">內層標籤的name對應的是外層標籤的id,必定要加上property="value"
值:${childm}
</logic:iterate>
</logic:iterate>
package com.jackey.topic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
//循環遍歷map的方法
public class CircleMap {
public static void main(String[] args) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a", 1);
tempMap.put("b", 2);
tempMap.put("c", 3);
// JDK1.4中
// 遍歷方法一 hashmap entrySet() 遍歷
System.out.println("方法一");
Iterator it = tempMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
System.out.println("");
// JDK1.5中,應用新特性For-Each循環
// 遍歷方法二
System.out.println("方法二");
for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
}
System.out.println("");
// 遍歷方法三 hashmap keySet() 遍歷
System.out.println("方法三");
for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循環輸出key
System.out.println("key=" + obj + " value=" + tempMap.get(obj));
}
for (Iterator i = tempMap.values().iterator(); i.hasNext();) {
Object obj = i.next();
System.out.println(obj);// 循環輸出value
}
System.out.println("");
// 遍歷方法四 treemap keySet()遍歷
System.out.println("方法四");
for (Object o : tempMap.keySet()) {
System.out.println("key=" + o + " value=" + tempMap.get(o));
}
System.out.println("11111");
// java如何遍歷Map <String, ArrayList> map = new HashMap <String,
// ArrayList>();
System.out
.println("java 遍歷Map <String, ArrayList> map = new HashMap <String, ArrayList>();");
Map<String, ArrayList> map = new HashMap<String, ArrayList>();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
ArrayList arrayList = map.get(key);
for (Object o : arrayList) {
System.out.println(o + "遍歷過程"); } } System.out.println("2222"); Map<String, List> mapList = new HashMap<String, List>(); for (Map.Entry entry : mapList.entrySet()) { String key = entry.getKey().toString(); List<String> values = (List) entry.getValue(); for (String value : values) { System.out.println(key + " --> " + value); } } } }