Map

2018-01-11  本文已影响0人  bluewind1230
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Map<Integer, String> maps = new HashMap<>();
        maps.put(1,"张三");
        maps.put(2,"李四");
        maps.put(1,"王五");
        maps.put(null,"张三");

        //通过Iterator来遍历
        Set<Integer> set = maps.keySet();
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()){
            Integer key = iterator.next();
            System.out.println("Key:"+key+" values:"+maps.get(key));
        }

        //通过Map.Entry来遍历,可以修改key和values值
        Set<Map.Entry<Integer,String>> set1 = maps.entrySet();
        Iterator<Map.Entry<Integer,String>> iterator1 = set1.iterator();
        while (iterator1.hasNext()){
            Map.Entry<Integer,String> entry = iterator1.next();
            System.out.println("key:"+entry.getKey()+ " values:"+entry.getValue());
        }

    }
}

上一篇 下一篇

猜你喜欢

热点阅读