HashMap遍历方法

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);
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_26641781/article/details/79864594