本篇内容主要讲解“Map的介绍的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Map的介绍的使用方法”吧!
曾都网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
Map的架构

Map是一个映射接口,不同于List和Set的是他不继承于Collection接口,Map中存储的内容是键值对(key-value)。
AbstractMap 是继承于Map的抽象类,它实现了Map中的大部分API。其它Map的实现类可以通过继承AbstractMap来减少重复编码。
SortedMap 是继承于Map的接口。SortedMap中的内容是排序的键值对,排序的方法是通过比较器(Comparator)。
NavigableMap 是继承于SortedMap的接口。相比于SortedMap,NavigableMap有一系列的导航方法;如"获取大于/等于某对象的键值对"、“获取小于/等于某对象的键值对”等等。
Map的主要实现类是HashMap、LinkedHashMap、TreeMap、HashTable等。
继承于AbstractMap
保存无序的键值对
非线程安全,元素可为null
继承于HashMap
保存有序的键值对,默认提供插入时的顺序,也可构造成访问顺序。
非线程安全,元素不可为null
继承于AbstractMap,且实现了NavigableMap接口
保存有序的键值对,默认对key排序,也可构造自定义的排序。
非线程安全,元素不可为null
但它继承于Dictionary,而且也实现Map接口
保存无序的键值对
线程安全,元素可为null
Map的源码解析
interface Entry{ K getKey(); V getValue(); V setValue(V value); boolean equals(Object o); int hashCode(); public static , V> Comparator > comparingByKey() { return (Comparator > & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); } public static > Comparator > comparingByValue() { return (Comparator > & Serializable) (c1, c2) -> c1.getValue().compareTo(c2.getValue()); } public static Comparator > comparingByKey(Comparator super K> cmp) { Objects.requireNonNull(cmp); return (Comparator > & Serializable) (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); } public static Comparator > comparingByValue(Comparator super V> cmp) { Objects.requireNonNull(cmp); return (Comparator > & Serializable) (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); } }
/** 返回当前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的集合 **/ SetkeySet(); /** 返回Map中value的集合 **/ Collection values(); /** 返回Map中key-value的集合 **/ Set > entrySet();
/** 根据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 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 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的介绍的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分享文章:Map的介绍的使用方法
当前链接:http://www.jxjierui.cn/article/ghcges.html


咨询
建站咨询
