本篇内容主要讲解“Map的介绍的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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
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中键值对的数量 **/
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();
/** 根据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的介绍的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/qgmzzmn/blog/3100968