• JAVA集合04_Map接口概述、常用方法、排序、Hashtable面试题


    ①. Map接口的概述

    • ①. Map是双列集合与Collection的单列集合并列存在

    • ②. Map 这里的K 、V 都应该是引用数据类型

    • ③. HashSet底层依赖HashMap,单列底层依赖双列集合
      在这里插入图片描述在这里插入图片描述

    • ④. Map中的key可以为null,value也可以为null,注意key只能有一个null,value可以有多个null

    ②. Map常用方法

    • ①. 添加功能:如果键是第一次储存,就直接储存元素,返回null;如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
      V put(K key ,V value):添加元素,返回的是以前的值
            Map<String,Integer> map=new HashMap<>();
            Integer i1=map.put("张三",23);
            Integer i2=map.put("李四",23);
            Integer i3=map.put("王五",24);
            Integer i4=map.put("赵六",25);
            Integer i5=map.put("张三",26);//相同的键不储存,把原来的值覆盖 把被覆盖的值返回
    
    
            System.out.println(map);//{李四=23, 张三=26, 王五=24, 赵六=25}
    
            //这里的i1--i4 都是null是因为开始的时候[张三,23][李四,23]....
            // 在集合中没有,这样把原来的值覆盖了,返回的是原来的值
            System.out.println(i1);//null
            System.out.println(i2);//null
            System.out.println(i3);//null
            System.out.println(i4);//null
            System.out.println(i5);//23
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • ②. 删除功能
    1. void clear():移除所有的键值对元素
    2. V remove(Object key):根据键删除键值对元素,并把值返回
           Map<String,Integer> map=new HashMap<>();
           map.put("张三",23);
           map.put("李四",23);
           map.put("王五",24);
           map.put("赵六",25);
           //根据键删除元素,返回键对应的值
           Integer value=map.remove("张三");
           System.out.println(value);//23
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • ③. 判断功能
    1. boolean containsKey(Objecct key):判断是否包含指定的键
    2. boolean containsValue(Objecct value):判断是否包含指定的值
           //判断是否包含传入的键
           System.out.println(map.containsKey("张三"));//true
           //判断是否包含传入的值
           System.out.println(map.containsValue(23));//true
    
    • 1
    • 2
    • 3
    • 4
    • ④. 长度功能
      int size():返回集合中的键值对的个数
           Map<String,Integer> map=new HashMap<>();
           map.put("张三",26);
           map.put("李四",23);
           map.put("王五",24);
           map.put("赵六",25);
    
           Collection<Integer>coll=map.values();
           System.out.println(coll);//[23, 23, 24, 25]
           System.out.println(map.size());//4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • ⑤. 获取功能
    1. V get(Object key):根据键获取值
    2. Set keySet():获取集合中所有的键集合
    3. Collectionvalues:获取集合中所有值集合
    4. Set> entrySet():拿到所有的键值对对象
      K getKey():得到entrySet中的键
      V getValue():得到entrySet中的值
    		Map<String, Integer> map = new HashMap<>();
    		map.put("张三", 23);
    		map.put("李四", 24);
    		map.put("王五", 25);
    		map.put("赵六", 23);                
    
    • 1
    • 2
    • 3
    • 4
    • 5
            //V get(Object key):根据键获取值
            Integer i = map.get("张三");//26
            Integer i2=map.get("小智");//没有的话返回null
    
            //1.获取所有的键
            Set<String> keySet = map.keySet();
            //iterator遍历
            Iterator<String> iterator = keySet.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();//获取每一个键
                Integer value = map.get(key); //根据键获取值
                System.out.println(key + "=" + value);
            }
            System.out.println("--------------");
            //使用增强for循环
            for (String key : map.keySet()) {
               Intger value=map.get(key)System.out.println(key + "=" +value );
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    interface Inter{
        interface Inter2{
            public void show();
        }
    }
    //这里的Inter.Inter2 和Map.Entry一样的
    class Demo implements Inter.Inter2{
    
        @Override
        public void show() {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
            //2.根据键值对对象,获取键和值
            //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并储存在Set集合中
            Set<Map.Entry<String,Integer>>entrySet=map.entrySet();
            //获取每一个对象
            Iterator<Map.Entry<String,Integer>>it=entrySet.iterator();
            while(it.hasNext()){
                //获取每一个Entry对象
                //static  class Entry implements Map.Entry{}
                Map.Entry<String,Integer>en=it.next();//父类引用指向子类对象
                //Entryen=it.next(); 子类对象
                //根据键值对对象获取键
                String key=en.getKey();
                //根据键值对对象获取值
                Integer value=en.getValue();
                System.out.println(key+"="+value);
            }
    
            //增强for循环
            for(Map.Entry<String,Integer> en:map.entrySet()){
                System.out.println(en.getKey()+"="+en.getValue());
            }
           for(Entry<String,Integer> en:map.entrySet()){
                System.out.println(en.getKey()+"="+en.getValue());
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    ③. HashMap概述

    • ①. Map中的key用Set来存放,不允许重复,即同一个Map对象所对应的类,须重写hashCode( )和equals( )方法

    • ②. HashMap判断两个key相等的标准是:两个key通过equals()方法返回 true,hashCode值也相等

    • ③. HashMap判断两个value相等的标准是:两个value通过 equals() 方法返回true

    • ④. 注意:两个HashMap的hashcode相同,则他们的equal()方法不一定相同,若两个HashMap的equal()相同,则他们的hashcode一定相同

    • ⑤. 允许使用null键和null值,与HashSet一样,不保证映射的顺序

    在这里插入图片描述

    • ⑥. HashMap集合键是Student值是String的案例
    // HashMap集合键是Student值是String的案例
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return age == student.age &&
                    Objects.equals(name, student.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name, age);
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 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
            HashMap<Student,String>hm=new HashMap<>();
            hm.put(new Student("张三",23),"北京");
            String i=hm.put(new Student("张三",23),"上海");
            hm.put(new Student("李四",24),"广州");
            hm.put(new Student("王五",25),"深圳");
            System.out.println(i);
            //没有重写equals方法之前,调用的是Object类的,会比较地址值
            //没有重写equals方法和 hashCode方法的时候map中的元素有4个,重写后只有3个
            //注意这里的张三对应的值是上海,因为把北京覆盖了
            System.out.println(hm);//北京
            //{Student{name='张三', age=23}=上海, Student{name='李四', age=24}=广州, Student{name='王五', 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ④. LinkedHashMap

    • LinkedHashMap:使用链表维护添加进Map中的顺序, 故遍历Map时,是按添加的顺序遍历的
    	LinkedHashMap<String,Integer>lhm=new LinkedHashMap<>();
    	lhm.put("张三",23);
    	lhm.put("李四",24);
    	lhm.put("王五",25);
    	lhm.put("赵六",26);
    	System.out.println(lhm);//{张三=23, 李四=24, 王五=25, 赵六=26}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ⑤. TreeMap排序

    • ①. TreeMap和TreeSet一样

    • ②. 自然排序:TreeMap 的所有的Key必须实现Comparable 接口,调用对象的comparaTo()方法和集合中的对象进行比较,根据CompableTo()方法返回的结果进行储存(所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException)

    • ③. 选择器排序:创建TreeMap时,传入一个Comparator对象,该对象负责对TreeMap中的所有 key进行排序。此时不需要Map的Key实现Comparable接口

    • ④. TreeMap集合键是Student值是String的案例

    //自然排序
    public class Student implements Comparable<Student> {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        @Override
        public int compareTo(Student s) {
            //以年龄为主要条件,姓名为次要条件
            int num=this.age-s.age;
            return num==0?this.name.compareTo(s.name):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
    • 42
    • 43
    • 44
    • 45
             TreeMap<Student,String> tm=new TreeMap<>();
             tm.put(new Student("张三",23),"北京");
             tm.put(new Student("李四",13),"上海");
             tm.put(new Student("王五",33),"广州");
             tm.put(new Student("赵六",43),"北京");
             System.out.println(tm);
             //{Student{name='李四', age=13}=上海, Student{name='张三', age=23}=北京,
            // Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
      //选择器排序
      TreeMap<Student,String> tm=new TreeMap<>(new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    //按照姓名比较
                    int num=s1.getName().compareTo(s2.getName());
                    return num==0?s1.getAge()-s2.getAge():num;
                }
            });
            tm.put(new Student("张三",23),"北京");
            tm.put(new Student("李四",13),"上海");
            tm.put(new Student("王五",33),"广州");
            tm.put(new Student("赵六",43),"北京");
            System.out.println(tm);
            //{Student{name='张三', age=23}=北京, Student{name='李四', age=13}=上海,
            //Student{name='王五', age=33}=广州, Student{name='赵六', age=43}=北京}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ⑥. Hashtable面试题

    HashMapHashtable的区别
    共同点:
    底层都是哈希算法,都是双列集合
    区别
    1.HashMap是线程不安全的,效率高,jdk1.2版本
      Hashtable是现场安全的,效率低,jdk1.0版本
    2.HashMap可以存储null键和nullHashtable不可以存储null键和null
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    TMS FMX Cloud提供集成元素
    断言语句 assertion
    记一次Python操作Excel——从入门到上手
    3.1_2 覆盖与交换
    浅谈pytorch安装
    草莓熊python turtle绘图代码
    【Linux】线程属性的定义&如何修改线程属性(附图解与代码实现)
    物联网毕设 --(STM32f407连接云平台检测数据)
    vue3 watch 异步方法
    阿里提出MS-Diffusion:一键合成你喜爱的所有图像元素,个性化生成新思路!
  • 原文地址:https://blog.csdn.net/TZ845195485/article/details/126084008