Map集合

java.util.Map<K,V>集合

Map集合的特点:
 1.双列集合,每个元素由两部分组成,通过键可以找到对应的值
 2.Map集合中的元素,key和value的数据类型可以相同,也可不同
 3.Map集合中的元素, K:键 ----键不可以重复 V:值----值可以重复
 4.Map集合中的元素,key与value一一对应

Map.Entry<K, V>即 Map 中定义的 K 类型的 key 和 V 类型的 value 的映射关系的类。
Map集合的常用方法:

 1.V put(K key, V value) 设置 key 对应的 value
  返回值V:
   存储键值对的时候,key不重复,返回值为null
   存储键值对的时候,key重复,会用新的value替换ap中重复的value,返回被替换的value

private static void shoe01() {
    //创建Map集合对象,多态
    Map<String,String>map=new HashMap<>();
    //1.V put(K key, V value)
    String v1=map.put("abc","def");
    System.out.println("v1:"+v1);//v1:null  key不重复,返回null
    String v2=map.put("abc","efg");
    System.out.println("v2:"+v2);//v2:def   key重复,用新的value替换ap中重复的value,返回被替换的value
    map.put("hig","klm");
    System.out.println(map);//  {abc=efg, hig=klm}--打印的不是地址,所以重写啦toString方法

}

 2.V remove(Object key) 删除 key 对应的映射关系
   返回值V
   删除时,key存在,返回被删除的值
   删除时,key存在,返回null

private static void show02() {
    Map<String,Integer>map=new HashMap<>();
    map.put("赵丽颖",168);
    map.put("杨颖",165);
    map.put("林志玲",178);
    System.out.println(map);//{林志玲=178, 赵丽颖=168, 杨颖=165}
    Integer v1=map.remove("林志玲");
    System.out.println("v1:"+v1);//v1:178
    System.out.println(map);//{赵丽颖=168, 杨颖=165}
    Integer v2=map.remove("林志颖");
    System.out.println("v2"+v2);//v2null
    System.out.println(map);//{赵丽颖=168, 杨颖=165}
}

 3.K getKey()返回 entry 中的 key
  返回值V
  key存在,返回value
  key不存在,返回null

private static void show03() {
    Map<String,Integer>map=new HashMap<>();
    map.put("赵丽颖",168);
    map.put("杨颖",165);
    map.put("林志玲",178);
    System.out.println(map);//{林志玲=178, 赵丽颖=168, 杨颖=165}
    System.out.println(map.get("赵丽颖"));//168
    System.out.println(map.get("黎明"));//null
}

  4.boolean containsKey(Object key) 判断是否包含 key
  返回值:boolean
  包含返回true
  不包含返回false

private static void show04() {
    Map<String,Integer>map=new HashMap<>();
    map.put("赵丽颖",168);
    map.put("杨颖",165);
    map.put("林志玲",178);
    System.out.println(map);//{林志玲=178, 赵丽颖=168, 杨颖=165}
    System.out.println(map.containsKey("林志玲"));//true
    System.out.println(map.containsKey("小米"));//false
}

5.boolean containsValue(Object value) 判断是否包含 value
6.V getValue() 返回 entry 中的 value
7.V get(Object key) 返回 key 对应的 value
8.V getOrDefault(Object key, V defaultValue) 返回 key 对应的 value,key 不存在,返回默认值
9. keySet() 返回所有 key 的不重复集合
10.Collection values() 返回所有 value 的可重复集合
11.Set<Map.Entry<K, V>> entrySet() 返回所有的 key-value 映射关系

Map的遍历:

第一种:通过键找值
   Map集合中的方法:
   Setkeyset()返回这次要映射中包含的键的Set图
   实现步骤:
   1.使用map集合中的keySet(),把Map集合所有的key取出来,存到Set集合中
   2.遍历set集合,获取Map中的每一个key
   3.通过Map集合中的方法get(key),通过key找到value
在这里插入图片描述

public class DemoKeySet {
    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("赵丽颖",168);
        map.put("杨颖",165);
        map.put("林志玲",178);
        //1.使用map集合中的keySet(),把Map集合所有的key取出来,存到Set集合中
        Set<String>set =map.keySet();
        //2.遍历set集合,获取Map中的每一个key(迭代器遍历/增强for遍历)
            //迭代器
            Iterator<String>it=set.iterator();
            while(it.hasNext()){
                String key=it.next();
                //3.通过Map集合中的方法get(key),通过key找到value
                Integer value=map.get(key);
                System.out.println(value);
            }
            //增强for
            for (String s : set) {
                Integer value=map.get(s);
                System.out.println(value);
            }
    }
}

第二种方式:使用Entry对象遍历
  Map集合中的方法:
  Set<Map<K,V>>entrySet()返回此映射中包含的键的Set图
  实现步骤:
1.使用map集合中的entrySet(),把Map集合所有的Entry对象取出来,存到Set集合中
2.遍历set集合,获取每一个Entry对象
3.通过Entry对象中的方法getKey(),getValue()获取键与值
在这里插入图片描述

public class DemoEntrySet {
    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("赵丽颖",168);
        map.put("杨颖",165);
        map.put("林志玲",178);
        Set<Map.Entry<String,Integer>> set=map.entrySet();
            //迭代器
            Iterator<Map.Entry<String,Integer>>it=set.iterator();
            while(it.hasNext()){
                Map.Entry<String,Integer>entry=it.next();
                String key=entry.getKey();
                Integer value=entry.getValue();
                System.out.println("key:"+key);
                System.out.println("value:"+value);
            }
            //增强for
            for (Map.Entry<String, Integer> entry : set) {
                String key=entry.getKey();
                Integer value=entry.getValue();
                System.out.println("key:"+key);
                System.out.println("value:"+value);
            }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值