Map

作者: bluewind1230 | 来源:发表于2018-01-11 09:29 被阅读0次
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());
        }

    }
}

相关文章

网友评论

      本文标题:Map

      本文链接:https://www.haomeiwen.com/subject/ouhznxtx.html