测试用的一个public class HashMapTest { private static Map temp = new HashMap();
public static void main(String[] args) {
for(int i=0; i<10; i++){
String str = "string" + i;
temp.put(new Integer(i), str);
}
for(Iterator it = temp.entrySet().iterator(); it.hasNext(); ){
Map.Entry e = (Map.Entry)it.next();
System.out.println("key: " + e.getKey());
System.out.println("value: " + e.getValue());
}
}
}
public static void main(String[] args) {
for(int i=0; i<10; i++){
String str = "string" + i;
temp.put(new Integer(i), str);
}
for(Iterator it = temp.entrySet().iterator(); it.hasNext(); ){
Map.Entry e = (Map.Entry)it.next();
System.out.println("key: " + e.getKey());
System.out.println("value: " + e.getValue());
}
}
}
因为键是一个集合, 值是一个集全,所以你可以把他们看成list或者set, 所以他们也是有迭代器方法
你可以理解成一个索引,通过key你会很快的找到你需要的值
Map<String, String>books = new HashMap<String, String>();
这里的泛型<String,String>
这个是什么含义
用一个泛型<String,String>这个我明白
用2个是?????
你这例子中的意思是分别将Map中的所有key值和value值取出来,
(Map的keySet() 与values()方法的返回值分别为Set,Collection容器引用的对象)
然后用迭代器遍历的方式将得到的容器里的值一个一个显示出来而已
建议多查查JDK!
2.引用和实际对象的泛型得统一,所以要用两个。
map包含一个键集合和值集合
分别可以迭代
一个键又对应一个值
要是3个呢????
这样可以吗?
http://blog.csdn.net/carefree31441