Map集合

概述:

  • 现实生活中,我们常会看到这样一种集合:IP地址和主机名,身份证号和个人,系统用户名与系统用户对象等,这种一一对应的关系,我们把它称之为映射。Java当中提供了专门的集合类用来存放这种映射关系的对象。即java.util.Map接口。
    在这里插入图片描述
  • Collection中的集合,元素是孤立存在的(单身),向集合中存储元素采用一个一个元素存储的方式进行存储。
  • Map中的集合,元素是成对存在的(夫妻),每个元素由键和值两部分组成。通过键可以找到对应的值。
  • Collection中的集合我们称之为单列集合,Map中的集合我们称之为双列集合
  • 注意:Map中的集合不能包含重复的键,值可以重复,每个键对应唯一一个值。
Map中的常用子类
  • 通过查看API帮助文档发现有很多个子类,我们主要介绍HashMap集合、LinkedHaspMap集合、HashTable集合
  • HashMap:存储数据采用的哈希表结构,元素的存取顺序可能不一致,由于要保证键的唯一性,不重复,需要重写键的hashCode方法和equals方法。
  • LinkedHashMap:HashMap下面有个子类LinkedHashMap,存储数据的方式是采用哈希表结构+链表结构,通过链表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证键的唯一、不重复,需要重写键的hashCode方法和equals方法。
  • 备注:Map接口当中,含有两个泛型变量,在使用时,要为两个泛型变量赋予数据类型,两个泛型变量的数据类型可以相同,也可以不同。
Map接口当中的常用API方法Map<K,V>

常用方法如下:

  • public V put(K key,V value):把指定的键与指定的值添加到Map集合当中
  • public V remove(Object key):把指定的键所对应的键值对元素从Map集合当中删除,返回被删除元素的值
  • public V get(Object key):根据指定的键 获得在Map集合当中对应的值
  • public Set keySet(): 获取Map集合当中的所有的key值,存储到Set集合当中
  • public Set<Map.Entry<K,V>> entrySet():获取到Map集合当中的所有的键值对对象的集合(Set集合)
    例子代码如下:
 public static void main(String[] args) {
        show03();
    }
    /**
     * public V remove(Object key);把指定的键所对应的键值对元素从Map集合中
     * key存在,返回的是删除的对应的value值
     * key不存在,返回null
     */
    public static  void show03(){
        Map<String,Integer> map = new HashMap<>();
        map.put("小孙", 20);
        map.put("小孙", 21);
        map.put("小王", 12);
        map.put("小赵", 21);
        map.put("小刘", 40);

        //使用get方法
        Integer v1 = map.get("小孙");
        System.out.println(v1);

        Integer v2 = map.get("老王");
        System.out.println(v2);

    }
    public static void show02(){
        Map<String,Integer> map = new HashMap<>();
        Integer v01 = map.put("小孙", 20);
        Integer v02 = map.put("小孙", 21);
        map.put("小王", 12);
        map.put("小赵", 21);
        map.put("小刘", 40);
        System.out.println("----------------------");
        System.out.println(map);

        //使用remove方法
        Integer v011 = map.remove("小孙");
        System.out.println(v011);
        System.out.println("----------------------");
        System.out.println(map);

        Integer v022 = map.remove("小李");
        System.out.println(v022);//null
        Integer int01 = 30;
        //int num01 = 30;
        //int i = int01.intValue();
        //建议使用包装类
    }
    /**
     * public V put(K key,V value):把指定的键与指定的值添加到Map集合当中
     * 返回值是V
     *          存储键值对的时候,key不重复,返回值V是null
     *          存储键值对的时候,key重复,会使用新的value值替换掉Map集合原来的value值,返回的是被替换的value值
     */
    public static void show01(){
        Map<String,String> map = new HashMap<>();
        String v01 = map.put("小孙", "小丽");
        System.out.println(v01);

        String v02 = map.put("小孙", "小花");
        System.out.println(v02);
        System.out.println("------------------------");
        System.out.println(map);//小孙=小花

        map.put("小王", "小丽");
        map.put("小赵", "小美");
        map.put("小刘", "小芳");
        System.out.println("------------------------");
        System.out.println(map);
    }
public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("迪丽热巴", 178);
        map.put("古力娜扎", 164);
        map.put("马尔扎哈", 160);
        map.put("撒由那拉", 150);
        //使用map集合当中keySet方法,
        Set<String> sets = map.keySet();
        //遍历set集合 采用迭代器 或者增强for循环
        Iterator<String> iterator = sets.iterator();
        while (iterator.hasNext()){
            String str = iterator.next();
            Integer value = map.get(str);
            System.out.println(str + " " + value);
        }
        System.out.println("-------------------------------");
        //使用增强for循环
        for (String set : sets) {
            Integer value = map.get(set);
            System.out.println(set + " " + value);
        }
        System.out.println("-------------------------------");
        for (String s : map.keySet()) {
            Integer va = map.get(s);
            System.out.println(s + " " + va);
        }

    }
/**
 * Map集合遍历的第二种方式:使用Entry对象遍历
 *
 * map集合中的方法:
 *          public Set<Map.Entry<K,V> entrySet():获取到Map集合当中的所有的键值对对象集合(Set集合)
 * 实现步骤:
 *      1、使用Map集合中的方法entrySet(),把Map集合当中的多个Entry对象取出来,存储到一个Set集合当中
 *      2、遍历Set集合,获取每一个Entry对象
 *      3、使用Entry对象中的方法getKey()和getValue()分别获取map集合当中的键与值
 */
public class Demo03EntrySet {
    public static void main(String[] args) {
        Double aDouble = Double.valueOf("123");
        System.out.println(aDouble);
        double v = Double.parseDouble("123");
        System.out.println(v);
        //创建一个Map集合对象
        Map<String,Integer> map = new HashMap<>();
        map.put("孙大圣", 160);
        map.put("者行孙", 160);
        map.put("孙行者", 170);
        map.put("行者孙", 175);
        //1、把Map集合当中的多个Entry对象取出来,存储到一个Set集合当中
        Set<Map.Entry<String, Integer>> set = map.entrySet();
       //2、遍历set集合,获取每一个Entry对象
        //使用迭代器遍历Set集合
        Iterator<Map.Entry<String, Integer>> iterator = set.iterator();
        while (iterator.hasNext()){
            //取出下一个元素
            Map.Entry<String, Integer> str = iterator.next();
            //3、使用Entry对象中的方法getKey()和getValue()分别获取map集合当中的键与值
            String key = str.getKey();
            Integer value = str.getValue();
            System.out.println(key + " " + value);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值