遍历
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();
例
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class testMap { public static void main(String[] args){ testMap test = new testMap(); HashMap map = new HashMap<String, String>(); map.put("a", "123"); map.put("a", "456"); map.put("b", "789"); test.transMap(map); } void transMap(HashMap map){ Iterator iter = map.entrySet().iterator(); while(iter.hasNext()){ Map.Entry entry =(Map.Entry)iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); System.out.println((String)key + ":" + (String)val); } } }
输出
a:456
b:789
dd