- 获取所有key,通过遍历key来遍历value
- Set<Map.Entry<K,V>> entrySet()
package collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest02 {
public static void main(String[] args) {
Map<Integer, String > map = new HashMap<>();
map.put(1,"11");
map.put(2,"22");
map.put(3,"33");
map.put(4,"44");
Set<Integer> keys = map.keySet();
Iterator<Integer> it = keys.iterator();
while (it.hasNext()){
Integer key = it.next();
String value = map.get(key);
System.out.println(value);
}
System.out.println("=========================");
for (Integer key : keys){
System.out.println(map.get(key));
}
System.out.println("=========================");
Set<Map.Entry<Integer, String>> set = map.entrySet();
Iterator<Map.Entry<Integer,String>> it2 = set.iterator();
while(it2.hasNext()){
Map.Entry<Integer, String> entry = it2.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}
System.out.println("=========================");
for (Map.Entry<Integer,String> node : set){
System.out.println(node.getKey() + "=" + node.getValue());
}
}
}
结果
11
22
33
44
=========================
11
22
33
44
=========================
1=11
2=22
3=33
4=44
=========================
1=11
2=22
3=33
4=44