zoukankan      html  css  js  c++  java
  • Map的遍历

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    
    /**
     * Description:Map遍历的map.keyset和map.entryset
     * @author 李XX2019年9月19日 
     */
    public class Map遍历
    {
        public static void main(String[] args)
        {
            Map<String,String> map = new HashMap<String,String>();
            map.put("1", "value1");
            map.put("2", "value2");
            map.put("3", "value3");
            map.put("4", "value4");
            map.put("5", "value5");
            
            System.out.println("通过map.keySet遍历key和value:");
            for(String key:map.keySet())
            {
                System.out.println("Key:"+key+" Value:"+map.get(key));
            }
            
            System.out.println("
    通过map.entrySet遍历key和value:");
            for(Map.Entry<String, String> entry:map.entrySet())
            {
                System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue());
            }
            
            System.out.println("
    通过map.entrySet使用Iteration遍历key和value");
            Iterator<Map.Entry<String, String>> it= map.entrySet().iterator();
            while(it.hasNext())
            {
                Map.Entry<String, String> entry=it.next();
                System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue());
            }
            
            System.out.println("
    通过map.keySet使用Iteration遍历key和value");
            Iterator<String> ite = map.keySet().iterator();
            while(ite.hasNext())
            {
                String key = ite.next();
                System.out.println("Key:"+key+" Value:"+map.get(key));
            }
            
            System.out.println("
    通过增强型for循环只遍历键或值");
            for(String s1:map.keySet())
            {
                System.out.println("Key:"+s1);
            }
            for(String s2:map.values())
            {
                System.out.println("Value:"+s2);
            }
        }
    }
  • 相关阅读:
    TODO 模板实践
    C++类继承方式及实践
    【转】C++友元
    C++面向对象实践
    数组指针实践
    引用&指针交换函数实践
    左值引用&右值引用实践【TODO】
    const变量的修改实践
    【转】c语言动态与静态分配
    【转】数组指针&指针数组
  • 原文地址:https://www.cnblogs.com/lhh666/p/11546360.html
Copyright © 2011-2022 走看看