public class HashMapTest {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
map.put(1,"华为");
map.put(2,"小米");
map.put(3,"苹果");
map.put(4,"三星");
for(Map.Entry<Integer,String> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
map.forEach((k,v)->{
System.out.println(k+":"+v);
});
for(Integer key:map.keySet()){
System.out.println(key+map.get(key));
}
for (Iterator<Integer> key = map.keySet().iterator();key.hasNext();){
Integer i = key.next();
String value = map.get(i);
System.out.println(i+":"+value);
}
}
}
HashMap遍历方法
最新推荐文章于 2023-03-01 22:38:44 发布