Map常用集合遍历

Map集合遍历的四种方式理解和简单使用

Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值

1:无非就是通过map.keySet()获取到值,然后根据键获取到值

  for(String s:map.keySet()){
            System.out.println("key : "+s+" value : "+map.get(s));
     }

2:通过Map.Entry(String,String) 获取,然后使用entry.getKey()获取到键,通过entry.getValue()获取到值

  for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
        }

3:其中通过Iterator也是为了最终获得entry,所以理解其用法,可以很好的使用和掌握

public class MapDemo {

    public static void main(String[] args) {
        Map<String, String> map=new HashMap<String, String>();
        map.put("张三1", "男");
        map.put("张三2", "男");
        map.put("张三3", "男");
        map.put("张三4", "男");
        map.put("张三5", "男");
        
        //第一种遍历map的方法,通过加强for循环map.keySet(),然后通过键key获取到value值
        for(String s:map.keySet()){
            System.out.println("key : "+s+" value : "+map.get(s));
        }
        System.out.println("====================================");
        
        //第二种只遍历键或者值,通过加强for循环
        for(String s1:map.keySet()){//遍历map的键
            System.out.println("键key :"+s1);
        }
        for(String s2:map.values()){//遍历map的值
            System.out.println("值value :"+s2);
        }
        System.out.println("====================================");    
        
        //第三种方式Map.Entry<String, String>的加强for循环遍历输出键key和值value
        for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
        }
        System.out.println("====================================");
        
        //第四种Iterator遍历获取,然后获取到Map.Entry<String, String>,再得到getKey()和getValue()
        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("====================================");
        
    }
    
    
}

4:Map的一些常用的知识点,和取值的变形形式

public class MapDemo {
    public static void main(String[] args) {
        //1:key,value都是object类型的
        //2:key必须是唯一的,不唯一,那么后面的value会把前面的value覆盖
        //3:对于HashMap,key可以为空
        //4:value可以为空,也可以为空
        //5:HashTable的key和value不能为空
        //6:properties的key和value必须为String类型的
        Map<String , String> map=new HashMap<>();
        map.put("null", "this is null 1");
        map.put("null", "this is null 2");
        System.out.println(map.size());
        System.out.println(map.get("null"));
        
        System.out.println("=============================");
        //循环显示map类型的key以及对应的value
        //三个集合,key的集合,value的集合,键值对的集合
        Set<String> keys=map.keySet();
        for(String s:keys){
            System.out.println(s);
        }
        System.out.println("=============================");
        Collection<String> values=map.values();//值的集合
        System.out.println(values);
        System.out.println("=============================");
        Set<Map.Entry<String, String>> entrys=map.entrySet();//键值对的集合
        for(Map.Entry<String, String> entry:entrys){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
        
    }
}

运行结果:

Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值

1:无非就是通过map.keySet()获取到值,然后根据键获取到值

  for(String s:map.keySet()){
            System.out.println("key : "+s+" value : "+map.get(s));
     }

2:通过Map.Entry(String,String) 获取,然后使用entry.getKey()获取到键,通过entry.getValue()获取到值

  for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
        }

3:其中通过Iterator也是为了最终获得entry,所以理解其用法,可以很好的使用和掌握

public class MapDemo {

    public static void main(String[] args) {
        Map<String, String> map=new HashMap<String, String>();
        map.put("张三1", "男");
        map.put("张三2", "男");
        map.put("张三3", "男");
        map.put("张三4", "男");
        map.put("张三5", "男");
        
        //第一种遍历map的方法,通过加强for循环map.keySet(),然后通过键key获取到value值
        for(String s:map.keySet()){
            System.out.println("key : "+s+" value : "+map.get(s));
        }
        System.out.println("====================================");
        
        //第二种只遍历键或者值,通过加强for循环
        for(String s1:map.keySet()){//遍历map的键
            System.out.println("键key :"+s1);
        }
        for(String s2:map.values()){//遍历map的值
            System.out.println("值value :"+s2);
        }
        System.out.println("====================================");    
        
        //第三种方式Map.Entry<String, String>的加强for循环遍历输出键key和值value
        for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
        }
        System.out.println("====================================");
        
        //第四种Iterator遍历获取,然后获取到Map.Entry<String, String>,再得到getKey()和getValue()
        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("====================================");
        
    }
    
    
}

4:Map的一些常用的知识点,和取值的变形形式

public class MapDemo {
    public static void main(String[] args) {
        //1:key,value都是object类型的
        //2:key必须是唯一的,不唯一,那么后面的value会把前面的value覆盖
        //3:对于HashMap,key可以为空
        //4:value可以为空,也可以为空
        //5:HashTable的key和value不能为空
        //6:properties的key和value必须为String类型的
        Map<String , String> map=new HashMap<>();
        map.put("null", "this is null 1");
        map.put("null", "this is null 2");
        System.out.println(map.size());
        System.out.println(map.get("null"));
        
        System.out.println("=============================");
        //循环显示map类型的key以及对应的value
        //三个集合,key的集合,value的集合,键值对的集合
        Set<String> keys=map.keySet();
        for(String s:keys){
            System.out.println(s);
        }
        System.out.println("=============================");
        Collection<String> values=map.values();//值的集合
        System.out.println(values);
        System.out.println("=============================");
        Set<Map.Entry<String, String>> entrys=map.entrySet();//键值对的集合
        for(Map.Entry<String, String> entry:entrys){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
        
    }
}

运行结果:

猜你喜欢

转载自www.cnblogs.com/ya-qiang/p/9327640.html