• 集合—Map接口和常用方法


    本次博客带领大家学习集合中的Map接口和常用方法。
    请添加图片描述

    Map接口实现类的特点

    1. Map与Collection并列存在。用于保存具有映射关系的数据:Key-Value。
    2. Map 中的 Key 和 Value 可以是任何引用类型的数据,会封装到HashMap$Node对象中。
    3. Map 中的key不允许重复。
    4. Map 中的value可以重复。
    5. Map 的key 可以为null,value 也可以为null,注意 key 为null,只能有一个,value 为null,可以多个。
    6. 常用String类作为Map的key。
    7. key 和 value 之间存在单向一对一关系,即通过指定的key总能找到对应的value。
    public class Map_ {
        public static void main(String[] args) {
            //接口实现类的特点
            //1.Map与Collection并列存在。用于保存具有映射关系的数据:Key-Value。
            Map map = new HashMap();
            map.put("no1","领导");
            map.put("no2","张三");
            map.put("no1","李四");//当有相同的k,就等价于替换。
            map.put("no3","李四");
            map.put(null,null);
            map.put(null,"abc");
            map.put("no4",null);
            map.put("no5",null);
            map.put(1,"王五");
            map.put(new Object(),"大黄");
            //通过get方法,传入key,会返回对应的value
            System.out.println(map.get("no2"));
            System.out.println("map="+map);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. Map存放数据的key-value示意图,一对k-v是放在一个HashMap$Node中的,有因为Node 实现了 Entry接口,有些书上也说一对k-v就是一个Entry。
      请添加图片描述
    public class MapSource_ {
        public static void main(String[] args) {
            Map map = new HashMap();
            map.put("no1","领导");
            map.put("no2","张三");
    
            /*
                1.k-v 最后是存放在HashMap$Node node = newNode(hash, key, value, null)
                2.k-v 为了方便程序员的遍历,还会 创建 EntrySet 集合,该集合存放的元素的类型Entry,
                而一个Entry对象就有k,v EntrySet> 即:transient Set> entrySet
               3.entrySet 中,定义的类型是 Map.Entry,但是实际上存放的还是 HashMap$Node
               这时因为 static class Node implements Map.Entry
               4.当把 HashMap$Node 对象存放到 entrySet 就方便我们的遍历,因为 Map.Entry 提供了重要方法
               K getKey();  V getValue();
             */
    
            Set set = map.entrySet();
            System.out.println(set.getClass()); //HashMap$EntrySet
            for (Object obj:set){
                System.out.println(obj.getClass());//HashMap$Node
                //为了从 HashMap$Node 取出 k-v
                //1. 先做一个向下转型
                Map.Entry entry = (Map.Entry) obj;
                System.out.println(entry.getKey()+"-"+entry.getValue());
            }
            Set set1 = map.keySet();
            System.out.println(set1.getClass());
            Collection values = map.values();
            System.out.println(values.getClass());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    Map接口常用方法

    1. put:添加
    2. remove:根据键删除映射关系
    3. get:根据键获取值
    4. size:获取元素个数
    5. isEmpty:判断个数是否为0
    6. clear:清除
    7. containsKey:查找键是否存在
    public class MapMethod {
        public static void main(String[] args) {
            //演示map接口常用方法
            Map map =new HashMap();
            map.put("邓超",new Book("",100));
            map.put("邓超","孙俪");
            map.put("王宝强","马蓉");
            map.put("宋喆","马蓉");
            map.put("刘令博",null);
            map.put(null,"刘亦菲");
            map.put("鹿晗","关晓彤");
    
            System.out.println("map="+map);
    //        1. remove:根据键删除映射关系
            map.remove(null);
            System.out.println("map="+map);
    //        2. get:根据键获取值
            Object val = map.get("鹿晗");
            System.out.println("val="+val);
    //        3. size:获取元素个数
            System.out.println("k-v="+map.size());
    //        4. isEmpty:判断个数是否为0
            System.out.println(map.isEmpty());
    //        5. clear:清除
           map.clear();
           System.out.println("map="+map);
    //        6. containsKey:查找键是否存在
            System.out.println(map.containsKey("鹿晗"));
    
        }
    }
    
    class Book{
        private String name;
        private int num;
    
        public Book(String name, int num) {
            this.name = name;
            this.num = num;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Map接口遍历方法

    1. containsKey:查找键是否存在
    2. keySet:获取所有的键
    3. entrySet:获取所有关系k-v
    4. values:获取所有的值

    方式一:先取出 所有的Key,通过Key取出对应的value

    Set keyset = map.keySet();
    //(1)增强for
    System.out.println("-----第一种方式的增强for-------");
    for (Object key:keyset) {
        System.out.println(key +"-"+map.get(key));
    }
    //(2)迭代器
    System.out.println("-----第一种方式的迭代器--------");
    Iterator iterator = keyset.iterator();
    while (iterator.hasNext()) {
        Object key =  iterator.next();
        System.out.println(key+"-"+map.get(key));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    方式二:把所有的values取出

    Collection values = map.values();
    //这里可以使用所有的Collections使用的遍历方法
    //(1)增强for
    System.out.println("-----第二种方式的增强for----");
    for (Object value :values) {
        System.out.println(value);
    }
    //(2)迭代器
    System.out.println("-----第二种方式的迭代器----");
    Iterator iterator1 = values.iterator();
    while (iterator1.hasNext()) {
        Object value =  iterator1.next();
        System.out.println(value);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    方式三:通过EntrySet 来获取k-v

    Set entrySet = map.entrySet();
    //(1)增强for
    System.out.println("-----使用EntrySet 的 for增强----");
    for (Object entry : entrySet){
        //将entry 转成 Map.Entry
        Map.Entry m = (Map.Entry) entry;
        System.out.println(m.getKey()+"-"+m.getValue());
    }
    System.out.println("-----使用EntrySet 的 迭代器----");
    //(2) 迭代器
    Iterator iterator2 = entrySet.iterator();
    while (iterator2.hasNext()) {
        Object next =  iterator2.next();
        //System.out.println(next.getClass());
        //向下转型 Map.Entry
        Map.Entry m = (Map.Entry) next;
        System.out.println(m.getKey()+"-"+m.getValue());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Map的练习

    使用HashMap添加3个员工对象,要求

    键:员工id

    值:员工对象

    并遍历显示工资>18000的员工(遍历方式最少两种)

    员工类:姓名、工资、员工id

    public class MapExercise {
        public static void main(String[] args) {
            Map hashMap = new HashMap();
            //添加对象
            hashMap.put(1,new Emp("jack",300000,1));
            hashMap.put(2,new Emp("tom",21000,2));
            hashMap.put(3,new Emp("milan",12000,3));
    
            //遍历2钟方式
            //1.使用keySet -> 增强for
            Set keySet = hashMap.keySet();
            System.out.println("====第一种遍历方式====");
            for (Object key : keySet) {
                //先获取value
                Emp emp = (Emp) hashMap.get(key);
                if(emp.getSal()>18000){
                    System.out.println(emp);
                }
            }
            //2.使用EntrySet ->迭代器
            Set entrySet = hashMap.entrySet();
            Iterator iterator = entrySet.iterator();
            while (iterator.hasNext()) {
                Map.Entry entry =  (Map.Entry)iterator.next();
                Emp emp = (Emp)entry.getValue();
                if(emp.getSal()>18000){
                    System.out.println(emp);
                }
            }
        }
    }
    
    class Emp{
        private String name;
        private double sal;
        private int id;
    
        public Emp(String name, double sal, int id) {
            this.name = name;
            this.sal = sal;
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getSal() {
            return sal;
        }
    
        public void setSal(double sal) {
            this.sal = sal;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "Emp{" +
                    "name='" + name + '\'' +
                    ", sal=" + sal +
                    ", id=" + id +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    sqlmap中文文档
    Jenkins
    软考-面向对象技术
    Swift 如何从图片数据(Data)检测原图片类型?
    【探花交友】阿里云OSS、百度人脸识别
    C. Keshi Is Throwing a Party- Codeforces Global Round 17
    用户运营中,怎么做好用户增长?
    并行化优化KD树算法:使用C#实现高效的最近邻搜索
    MasaFramework的MinimalAPI设计
    【7.5】Codeforces 刷题
  • 原文地址:https://blog.csdn.net/lidong777777/article/details/126065139