Java集合-----Map的用法

 

Map接口简介

今天来看一看map集合,map映射接口,用于存放键值对,<key,value>,通过key来查找value,顾名思义key不能为空,唯一且不重复,不然底层怎么查呢!

可以从图中看出Map为单独的接口,他和Collection有什么区别呢?

Map和Collection在集合中并列存在。 
Map集合是双列的,键值对,而Collection是单列集合
Map存储元素使用put方法,Collection使用Put方法。
Map遍历没有直接取出元素的方法,而是先转成Set集合,再通过迭代获取元素。
 --Map常用方法

注意:Set的元素不可重复,Map的键不可重复,如果存入重复元素如何处理

Set元素重复元素不能存入add方法返回false

Map的重复健将覆盖旧键,将旧值返回。

  
–Map应用
添加:使用HashMap。立了学生姓名和年龄之间的映射关系。并试图添加重复的键


   public static void main(String[] args) {
        // 定义一个Map的容器对象  
        Map<String, Integer > map1 = new HashMap<String, Integer >();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);
      // map1.put("jack", 30); 在没有hashCode和equals方式   添加重复的键值(值不同),会覆盖掉前面key值相同的值
        System.out.println(map1);  
      
        Map<String, Integer> map2 = new HashMap<String, Integer>();  
        map2.put("张三丰", 100);  
        map2.put("虚竹", 20);  
        System.out.println("map2:" + map2);  
        // 从指定映射中将所有映射关系复制到此映射中。  
        map1.putAll(map2);  
        System.out.println("map1:" + map1);  
    }  

删除:


   public static void main(String[] args) {   
   // 删除:  
        // remove() 删除关联对象,指定key对象  
        // clear() 清空集合对象  
  
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        System.out.println(map1);                 
        // 指定key,返回删除的键值对映射的值。  
        map1.remove("java");
        System.out.println(map1);  
        map1.clear();  
        System.out.println("map1:" + map1);  
    }  


获取:


public static void main(String[] args) {
     // 获取:  
        // V get(Object key) 通过指定的key对象获取value对象  
        // int size() 获取容器的大小  
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        System.out.println(map1);  
        // V get(Object key) 通过指定的key对象获取value对象  
        System.out.println("value:" + map1.get("jack"));  
        // int size() 获取容器的大小
        System.out.println("map.size:" + map1.size()); 
    } 

判断:

复制代码
public static void main(String[] args) {
        // 判断:  
        // boolean isEmpty() 判断集合是否为空   长度为0返回true否则false  
        // boolean containsKey(Object key) 判断集合中是否包含指定的key  
        // boolean containsValue(Object value)  
  
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        System.out.println(map1);  
        System.out.println("isEmpty:" + map1.isEmpty());  
        System.out.println("containskey:" + map1.containsKey("jack"));  
        System.out.println("containsvalues:" + map1.containsValue(100));  
    }  
复制代码

遍历Map的4中方式:

第一种:

 

public static void main(String[] args) {
        //遍历Map 第一种方式
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        
        //通过 map1.keySet() 获取key  通过key 找到value
        for (String key : map1.keySet()) {
            Integer value = map1.get(key);
            System.out.println("key : "+key+" value : "+value);
        }
    }

第二种:


public static void main(String[] args) {
        //遍历Map 第二种方式
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        
       //通过Map.Entry(String,Integer) 获取,然后使用entry.getKey()获取到键,通过entry.getValue()获取到值
       for(Map.Entry<String, Integer> entry : map1.entrySet()){
           System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
       }
    }

第三种:


//遍历Map 第三种方式
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        //第三种只遍历键或者值,通过加强for循环
        for(String s1:map1.keySet()){//遍历map的键
           System.out.println("键key :"+s1);
        }
        for(Integer s2:map1.values()){//遍历map的值
            System.out.println("值value :"+s2);
        }
           System.out.println("====================================");    
    }

第四种:

复制代码
public static void main(String[] args) {
        //遍历Map 第一种方式
        Map<String, Integer> map1 = new HashMap<String, Integer>();  
        map1.put("jack", 20);  
        map1.put("rose", 18);  
        map1.put("lucy", 17);  
        map1.put("java", 25);  
        
        //第四种Iterator遍历获取,然后获取到Map.Entry<String, String>,再得到getKey()和getValue()
        Iterator<Map.Entry<String, Integer>> it=map1.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entry=it.next(); 
            System.out.println("键key :"+entry.getKey()+" value :"+entry.getValue());
        }
        
    }
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值