美文网首页
三种遍历HashMap的方法

三种遍历HashMap的方法

作者: 软萌白甜Hedy | 来源:发表于2019-09-29 19:38 被阅读0次
public class TraverseMapTest {
    public static void main(String[] args) {
        Map<String , String > map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        //1.keySet
        for(String str : map.keySet()){
            System.out.println("K:"+str+"  V:"+map.get(str));
        }
        //2.EntrySet
        for(Map.Entry<String, String> en: map.entrySet()){
            System.out.println("K:"+en.getKey()+"  V:"+en.getValue());
        }
        //3.EntrySet的Iterator方法
        Iterator<Map.Entry<String ,String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry entry = it.next();
            System.out.println("K:"+entry.getKey()+"  V:"+entry.getValue());
        }

    }


}

相关文章

网友评论

      本文标题:三种遍历HashMap的方法

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