Map的介绍的使用方法

发布时间:2021-06-22 15:49:03 作者:chen
来源:亿速云 阅读:114

本篇内容主要讲解“Map的介绍的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Map的介绍的使用方法”吧!

Map的架构

Map的介绍的使用方法

HashMap

LinkedHashMap

TreeMap

HashTable

Map的源码解析

Entry

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());
    }
}

Map中的方法

/** 返回当前Map中键值对的数量 **/
int size();

/** 返回当前Map是否为空 **/
boolean isEmpty();

/** 返回当前Map是否包含键key **/
boolean containsKey(Object key);

/** 返回当前Map是否包含值value **/
boolean containsValue(Object value);

/** 返回当前Map中键key对应的值value **/
V get(Object key);

/** 将当前Map中键key对应的值设置为value  **/
V put(K key, V value);

/** 移除当前Map中键key对应的值  **/
V remove(Object key);

/** 将m中所有键值对放到当前Map中 **/
void putAll(Map<? extends K, ? extends V> m);

/** 移除Map中所有键值对 **/
void clear();

/** 返回Map中key的集合 **/
Set<K> keySet();

/** 返回Map中value的集合 **/
Collection<V> values();

/** 返回Map中key-value的集合 **/
Set<Map.Entry<K, V>> entrySet();

Map中1.8新增的方法

/** 根据key获取value 如果value为空返回默认值defaultValue **/
default V getOrDefault(Object key, V defaultValue) {
    V v;
    return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue;
}

/** 函数式编程 遍历map中键值对 **/
default void forEach(BiConsumer<? super K, ? super V> action) {
    Objects.requireNonNull(action);
    for (Map.Entry<K, V> entry : entrySet()) {
        K k;
        V v;
        try {
            k = entry.getKey();
            v = entry.getValue();
        } catch(IllegalStateException ise) {
            // this usually means the entry is no longer in the map.
            throw new ConcurrentModificationException(ise);
        }
        action.accept(k, v);
    }
}

/** 函数式编程 提供一个方法 替换所有的value **/
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
    Objects.requireNonNull(function);
    for (Map.Entry<K, V> entry : entrySet()) {
        K k;
        V v;
        try {
            k = entry.getKey();
            v = entry.getValue();
        } catch(IllegalStateException ise) {
            // this usually means the entry is no longer in the map.
            throw new ConcurrentModificationException(ise);
        }

        // ise thrown from function is not a cme.
        v = function.apply(k, v);

        try {
            entry.setValue(v);
        } catch(IllegalStateException ise) {
            // this usually means the entry is no longer in the map.
            throw new ConcurrentModificationException(ise);
        }
    }
}

/** 如果map中key为空 则往map中放入key-value **/
default V putIfAbsent(K key, V value) {
    V v = get(key);
    if (v == null) {
        v = put(key, value);
    }

    return v;
}

/** 如果Map中的key对应的值是value 则移除此键值对 否则返回false **/
default boolean remove(Object key, Object value) {
    Object curValue = get(key);
    if (!Objects.equals(curValue, value) ||
            (curValue == null && !containsKey(key))) {
        return false;
    }
    remove(key);
    return true;
}

/** 如果Map中的key对应的值是oldValue 则用newValue替换oldValue 否则返回false **/
default boolean replace(K key, V oldValue, V newValue) {
    Object curValue = get(key);
    if (!Objects.equals(curValue, oldValue) ||
            (curValue == null && !containsKey(key))) {
        return false;
    }
    put(key, newValue);
    return true;
}

/** 如果Map中存在key键 则替换为value 否则返回false **/
default V replace(K key, V value) {
    V curValue;
    if (((curValue = get(key)) != null) || containsKey(key)) {
        curValue = put(key, value);
    }
    return curValue;
}

/** 如果Map中key对应的value为空 则将key计算后设置为key对应的value **/
default V computeIfAbsent(K key,
                          Function<? super K, ? extends V> mappingFunction) {
    Objects.requireNonNull(mappingFunction);
    V v;
    if ((v = get(key)) == null) {
        V newValue;
        if ((newValue = mappingFunction.apply(key)) != null) {
            put(key, newValue);
            return newValue;
        }
    }

    return v;
}

/** 如果Map中 key对应的oldValue为空 则返回null
 * 否则根据key和oldValue计算出新的newValue   如果newValue不为空则put到Map中 如果为空 则移除此键值对 **/
default V computeIfPresent(K key,
                           BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
    Objects.requireNonNull(remappingFunction);
    V oldValue;
    if ((oldValue = get(key)) != null) {
        V newValue = remappingFunction.apply(key, oldValue);
        if (newValue != null) {
            put(key, newValue);
            return newValue;
        } else {
            remove(key);
            return null;
        }
    } else {
        return null;
    }
}

/** 根据Map中的key和对应的oldValue计算出newValue
 * 如果newValue为空 且oldValue不为空 则移除此键值对
 * 如果newValue为空 且oldValue为空且不存在key 返回null
 * 如果newValue不为空 则put到Map中 **/
default V compute(K key,
                  BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
    Objects.requireNonNull(remappingFunction);
    V oldValue = get(key);

    V newValue = remappingFunction.apply(key, oldValue);
    if (newValue == null) {
        // delete mapping
        if (oldValue != null || containsKey(key)) {
            // something to remove
            remove(key);
            return null;
        } else {
            // nothing to do. Leave things as they were.
            return null;
        }
    } else {
        // add or replace old mapping
        put(key, newValue);
        return newValue;
    }
}

/** 根据Map中的key查找oldValue 如果oldValue为空 则设置newValue为oldValue  否则根据oldValue和value计算出newValue
 * 如果newValue为空 则移除此键值对  否则设置key的值为newValue **/
default V merge(K key, V value,
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    Objects.requireNonNull(remappingFunction);
    Objects.requireNonNull(value);
    V oldValue = get(key);
    V newValue = (oldValue == null) ? value :
            remappingFunction.apply(oldValue, value);
    if(newValue == null) {
        remove(key);
    } else {
        put(key, newValue);
    }
    return newValue;
}

到此,相信大家对“Map的介绍的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. JavaScript中map方法的使用介绍
  2. JavaScript中Map对象的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

map

上一篇:django中template如何使用

下一篇:TreeSet的介绍和使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》