java 中遍歷Map的幾種方法
阿新 • • 發佈:2017-12-07
叠代器 ofo popu arr highlight shm int print number
方法分為兩類:
一類是基於map的Entry;map.entrySet();
一類是基於map的key;map.keySet()
而每一類都有兩種遍歷方式:
a.利用叠代器 iterator;
b.利用for-each循環;
代碼舉例如下:
[java] view plaincopy
- package cn.wzb;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.Map.Entry;
- //循環遍歷map的方法
- public class TestMap {
- public static void main(String[] args) {
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- map.put("b", 2);
- map.put("c", 3);
- // JDK1.4中
- // 遍歷方法一 hashMap.entrySet() 遍歷
- System.out.println("方法一");
- Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String, Integer> entry = it.next();
- String key = entry.getKey();
- Integer 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 : map.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue().toString();
- System.out.println("key =" + key + " value = " + value);
- }
- System.out.println("");
- // 遍歷方法三 hashMap.keySet().iterator() 遍歷
- System.out.println("方法三");
- for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
- String key = i.next();
- System.out.println(" key = " + key + "; value = " + map.get(key));
- }
- /*
- for (Iterator<Integer> i = map.values().iterator(); i.hasNext(); ) {
- Integer value = i.next();
- System.out.println(value));// 循環輸出value
- }
- */
- /*
- for(Integer value : map.values() ) {
- System.out.println("value = " + value);
- }
- */
- System.out.println("");
- // 遍歷方法四 Hashmap.keySet()遍歷
- System.out.println("方法四");
- for (String key : map.keySet()) {
- System.out.println("key = " + key + "; value = " + map.get(key));
- }
- System.out.println();
- // java如何遍歷Map <String, ArrayList> map = new HashMap <String, ArrayList<Integer> >();
- System.out.println("java 遍歷Map <String, ArrayList<Integer> map = new HashMap <String, ArrayList<Integer>>();");
- System.out.println("方法一:");
- Map<String, List<Integer> > mapList = new HashMap <String, List<Integer> >();
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- mapList.put("aaa", list);
- mapList.put("bbb", list);
- Set<String> keys = mapList.keySet();
- Iterator<String> iterator = keys.iterator();
- while (iterator.hasNext()) {
- String key = iterator.next();
- System.out.println(key + " : ");
- List<Integer> arrayList = mapList.get(key);
- for (Integer i : arrayList) {
- System.out.print(i + ", ");
- }
- System.out.println();
- }
- System.out.println("方法二:");
- for (Map.Entry<String, List<Integer> > entry : mapList.entrySet()) {
- String key = entry.getKey();
- System.out.println(key + " : ");
- List<Integer> values = (List<Integer>) entry.getValue();
- for (Integer value : values) {
- System.out.print(value + ", ");
- }
- System.out.println();
- }
- }
- }
- package cn.wzb;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.Map.Entry;
- //循環遍歷map的方法
- public class TestMap {
- public static void main(String[] args) {
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- map.put("b", 2);
- map.put("c", 3);
- // JDK1.4中
- // 遍歷方法一 hashMap.entrySet() 遍歷
- System.out.println("方法一");
- Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String, Integer> entry = it.next();
- String key = entry.getKey();
- Integer 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 : map.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue().toString();
- System.out.println("key =" + key + " value = " + value);
- }
- System.out.println("");
- // 遍歷方法三 hashMap.keySet().iterator() 遍歷
- System.out.println("方法三");
- for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
- String key = i.next();
- System.out.println(" key = " + key + "; value = " + map.get(key));
- }
- /*
- for (Iterator<Integer> i = map.values().iterator(); i.hasNext(); ) {
- Integer value = i.next();
- System.out.println(value));// 循環輸出value
- }
- */
- /*
- for(Integer value : map.values() ) {
- System.out.println("value = " + value);
- }
- */
- System.out.println("");
- // 遍歷方法四 Hashmap.keySet()遍歷
- System.out.println("方法四");
- for (String key : map.keySet()) {
- System.out.println("key = " + key + "; value = " + map.get(key));
- }
- System.out.println();
- // java如何遍歷Map <String, ArrayList> map = new HashMap <String, ArrayList<Integer> >();
- System.out.println("java 遍歷Map <String, ArrayList<Integer> map = new HashMap <String, ArrayList<Integer>>();");
- System.out.println("方法一:");
- Map<String, List<Integer> > mapList = new HashMap <String, List<Integer> >();
- List<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- mapList.put("aaa", list);
- mapList.put("bbb", list);
- Set<String> keys = mapList.keySet();
- Iterator<String> iterator = keys.iterator();
- while (iterator.hasNext()) {
- String key = iterator.next();
- System.out.println(key + " : ");
- List<Integer> arrayList = mapList.get(key);
- for (Integer i : arrayList) {
- System.out.print(i + ", ");
- }
- System.out.println();
- }
- System.out.println("方法二:");
- for (Map.Entry<String, List<Integer> > entry : mapList.entrySet()) {
- String key = entry.getKey();
- System.out.println(key + " : ");
- List<Integer> values = (List<Integer>) entry.getValue();
- for (Integer value : values) {
- System.out.print(value + ", ");
- }
- System.out.println();
- }
- }
- }
java 中遍歷Map的幾種方法