1. 程式人生 > >用Iterator(Iterator)遍歷HasMap的值

用Iterator(Iterator)遍歷HasMap的值

public static void main(String[] args) {
        // TODO Auto-generated method stub

        Map<Integer, String> map = new HashMap<Integer, String>();

        map.put(5, "小黑");
        map.put(4, "小白");
        map.put(8, "小灰");
        map.put(7, "小紅");
        map.put(5, "小蘭");
        map.put
(10, "小醋"); map.remove(8); System.out.println(map.get(4) + "\t" + map.get(7)); System.out.println("map的長度是:" + map.size()); for (String v : map.values()) { System.out.println("values=" + v); } Iterator<Integer>iterator=map.keySet().iterator
(); while(iterator.hasNext()){ int key =iterator.next(); String value =map.get(key); System.out.println("key的值是:"+key+"\t"+"value的值是:"+value); }