• 【Java入门提高篇】Day19 Java容器类详解(二)Map接口


      上一篇里介绍了容器家族里的大族长——Collection接口,今天来看看容器家族里的二族长——Map接口。

      Map也是容器家族的一个大分支,但里面的元素都是以键值对(key-value)的形式存放的,就像字典一样,用相应的key就可以拿到相应的value。

      先来看看Map接口的内容,下面是阉割版的Map接口(去掉了default method),去掉的部分涉及Stream操作,属于Map的高级用法,所以暂时不做介绍。

    import java.io.Serializable;
    import java.util.Collection;
    import java.util.Comparator;
    import java.util.Objects;
    import java.util.Set;
    
    public interface Map<K,V> {
        // 查询操作
    
        /**
         * 返回键值对数量
         */
        int size();
    
        /**
         * Map是否为空
         */
        boolean isEmpty();
    
        /**
         * Map中是否包含指定的key
         */
        boolean containsKey(Object key);
    
        /**
         * Map中是否包含指定的value
         */
        boolean containsValue(Object value);
    
        /**
         * 根据key获取对应的value
         */
        V get(Object key);
    
        // Modification Operations
    
        /**
         * 插入键值对,如果Map中已经存在该key,则新的value会覆盖原来的value
         */
        V put(K key, V value);
    
        /**
         * 移除指定key对应的键值对,并返回相应的value
         */
        V remove(Object key);
    
    
        // 批量操作
    
        /**
         * 将另一个Map中的键值对全部复制过来
         */
        void putAll(Map<? extends K, ? extends V> m);
    
        /**
         * 移除所有键值对
         */
        void clear();
    
    
        // 视图
    
        /**
         * 返回包含Map中所有key的(Set类型)键视图,对Map的修改也会影响到键视图
         */
        Set<K> keySet();
    
        /**
         * 返回包含Map中所有value的(Collection类型)值视图,对Map的修改也会影响到值视图
         */
        Collection<V> values();
    
        /**
         * 返回包含Map中所有键值对的(java.util.Map.Entry类型)键值对视图
         */
        Set<Map.Entry<K, V>> entrySet();
    
        /**
         * Map 键值对接口
         */
        interface Entry<K,V> {
            /**
             * 返回键
             */
            K getKey();
    
            /**
             * 返回值
             */
            V getValue();
    
            /**
             * 设置键
             */
            V setValue(V value);
    
            boolean equals(Object o);
    
            int hashCode();
    
            /**
             * 键比较器(内部比较器)
             */
            public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
                return (Comparator<Map.Entry<K, V>> & Serializable)
                        (c1, c2) -> c1.getKey().compareTo(c2.getKey());
            }
    
            /**
             * 值比较器(内部比较器)
             */
            public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
                return (Comparator<Map.Entry<K, V>> & Serializable)
                        (c1, c2) -> c1.getValue().compareTo(c2.getValue());
            }
    
            /**
             * 键比较器(外部比较器)
             */
            public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
                Objects.requireNonNull(cmp);
                return (Comparator<Map.Entry<K, V>> & Serializable)
                        (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
            }
    
            /**
             * 值比较器(外部比较器)
             */
            public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
                Objects.requireNonNull(cmp);
                return (Comparator<Map.Entry<K, V>> & Serializable)
                        (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
            }
        }
    
        // 比较和散列
    
        boolean equals(Object o);
    
        int hashCode();
    }

      可以看到,Map接口的内容,其实比Collection接口更丰富,这里因为省略了很多高级方法,而且里面包含了另外一个接口,Map.Entry接口,也就是一直所说的键值对,这个接口是Map中元素需要实现的接口。

      Map有三种遍历方式:1.通过遍历KeySet来遍历所有键值对,2.通过遍历EntrySet来实现,3.通过EntrySet的Iterator来遍历。这里还有一个新概念——视图,视图其实就是一个集合,但是是一个不能修改的集合,只能对视图进行查询和遍历操作,在Map中一共有三个视图,键视图,值视图,键值对视图,下面可以看一个小栗子:

    public class Test {
        public static void main(String[] args){
            Map<Integer, Integer> map = new HashMap<>();
            map.put(1,11);
            map.put(2,22);
            map.put(3,33);
    
            Set<Integer> keys = map.keySet();
            Collection<Integer> values = map.values();
            Set<Map.Entry<Integer,Integer>> entries = map.entrySet();
            Iterator<Map.Entry<Integer,Integer>> iterator = entries.iterator();
            System.out.println(keys);
            System.out.println(values);
            System.out.println(entries);
    
            System.out.println("按keyset遍历");
            for (Integer key : keys){
                System.out.println("key:" + key + " value:" + map.get(key));
            }
    
            System.out.println("按键值对遍历");
            for (Map.Entry<Integer,Integer> entry : entries){
                System.out.println("entry:" + entry);
            }
    
            System.out.println("按iterator遍历");
            while (iterator.hasNext()){
                Map.Entry<Integer,Integer> entry = iterator.next();
                System.out.println("entry:" + entry);
            }
    
            map.put(2,444);
            map.put(4,44);
            System.out.println("修改后的视图");
            System.out.println(keys);
            System.out.println(values);
            System.out.println(entries);
    
            keys.add(5);
            values.add(55);
        }
    }

      输出如下:

    [1, 2, 3]
    [11, 22, 33]
    [1=11, 2=22, 3=33]
    按keyset遍历
    key:1 value:11
    key:2 value:22
    key:3 value:33
    按键值对遍历
    entry:1=11
    entry:2=22
    entry:3=33
    按iterator遍历
    entry:1=11
    entry:2=22
    entry:3=33
    修改后的视图
    [1, 2, 3, 4]
    [11, 444, 33, 44]
    [1=11, 2=444, 3=33, 4=44]

    Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractCollection.add(AbstractCollection.java:262)
    at com.frank.chapter19.Test.main(Test.java:44)

      栗子里介绍了三种遍历方式,也看到了三种视图的样子,当我们试图修改视图时,抛出了一个UnsupportedOperationException异常,表明该视图集合无法修改。

      在Map.Entry接口里,还可以看到外部比较器和内部比较器,这两个概念暂时也不做介绍,在之后的文章里会介绍。

      关于Map,要说的主要就这么多了,目前来说只需要知道Map是以键值对的形式进行存取,并了解Map接口中的主要方法及其作用,了解Map的遍历方法,和视图的概念就已经足够了。

      本篇到此结束,欢迎大家继续关注。

  • 相关阅读:
    链接Oracle数据库
    Spring boot Mybatis
    Spring Boot 部署
    javaEE应用组件
    maven项目搭建步骤
    Spring Boot中Redis的使用
    Struts2 Hello,Wold
    使用JSON
    Spring中Quartz的配置
    Guice示例
  • 原文地址:https://www.cnblogs.com/mfrank/p/9070160.html
Copyright © 2020-2023  润新知