/**
* MAP遍歷
*
* @author: NPF
* @date: 2018/2/27.
*/
public class MapUtil {
public static void main(String[] args) {
Map<String, Integer> tempMap = new HashMap<>();
tempMap.put("a", 111);
tempMap.put("b", 222);
tempMap.put("c", 333);
each1(tempMap);
each2(tempMap);
each3(tempMap);
each4(tempMap);
eachMapList();
}
/**
* JDK1.4
* Map entrySet() 遍歷
*
* @param tempMap
*/
private static void each1(Map<String, Integer> tempMap) {
Iterator it = tempMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
Integer value = (Integer) entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
}
/**
* JDK1.5中,應(yīng)用新特性For-Each循環(huán)
*
* @param tempMap
*/
private static void each2(Map<String, Integer> tempMap) {
for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
}
/**
* Map keySet() 遍歷
*
* @param tempMap
*/
private static void each3(Map<String, Integer> tempMap) {
/**
* 循環(huán)獲取key,通過key再獲取value
*/
for (Iterator i = tempMap.keySet().iterator(); i.hasNext(); ) {
String key = (String) i.next();
Integer value = tempMap.get(key);
System.out.println("key=" + key + " value=" + value);
}
/**
* 循環(huán)獲取value
*/
for (Iterator i = tempMap.values().iterator(); i.hasNext(); ) {
Integer value = (Integer) i.next();
System.out.println("value=" + value);
}
}
/**
* Map keySet()遍歷
*
* @param tempMap
*/
private static void each4(Map<String, Integer> tempMap) {
for (String key : tempMap.keySet()) {
Integer value = tempMap.get(key);
System.out.println("key=" + key + " value=" + value);
}
}
/**
* 遍歷Map <String, ArrayList> map = new HashMap <>();
*/
private static void eachMapList() {
/**
* 方式一
*/
Map<String, ArrayList<String>> map1 = new HashMap<>();
Set<String> keys = map1.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
ArrayList<String> arrayList = map1.get(key);
for (String value : arrayList) {
System.out.println(key + "==方式一==" + value);
}
}
/**
* 方式二
*/
Map<String, List<String>> map2 = new HashMap<>();
for (Map.Entry entry : map2.entrySet()) {
String key = entry.getKey().toString();
List<String> list = (List) entry.getValue();
for (String value : list) {
System.out.println(key + "==方式二==" + value);
}
}
}}
MAP遍歷
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 當(dāng)我頭一天晚上發(fā)現(xiàn)我們這里有一個(gè)東西要改一下,我發(fā)一個(gè)郵件出去,有的第二天上班的時(shí)候就發(fā)現(xiàn)這個(gè)東西改過來了,已經(jīng)上...