java map中套map_Java Map – Java中的Map

java map中套map

Java Map is part of collections framework. Java Map object is used to store key-value mappings. Java Map can’t contain duplicate keys however duplicate values are allowed.

Java Map是集合框架的一部分。 Java Map对象用于存储键值映射。 Java Map不能包含重复的键,但是允许重复的值。

Java地图 (Java Map)

java map, map in java, java map example, java map interface

Some of the important points about Map in java are;


Java中有关Map的一些重要要点是:

  1. Map provides three collection views – set of keys, set of key-value mappings and collection of values.

    Map提供了三个集合视图–键集,键-值映射集和值集合。
  2. Map doesn’t guarantee order of mappings, however it depends on the implementation. For example, HashMap doesn’t guarantee the order of mappings but TreeMap does.

    Map不保证映射的顺序,但是取决于实现。 例如,HashMap不能保证映射的顺序,但是TreeMap可以保证。
  3. Map utilise hashCode and equals methods on Key for get and put operations. So mutable classes are not a good fit for Map keys. If the values of hashCode or equals change after put, you won’t get the correct value in get operation.

    Map利用hashCode和Key上的equals方法进行获取和放置操作。 因此,可变类并不适合用作Map键。 如果放置后hashCode或equals的值发生变化,则get操作中将无法获得正确的值。
  4. Popular implementation classes of Map in Java are HashMap, Hashtable, TreeMap, ConcurrentHashMap and LinkedHashMap.

    Java中流行的Map实现类是HashMap,Hashtable,TreeMap,ConcurrentHashMap和LinkedHashMap。
  5. AbstractMap class provides skeletal implementation of the Map interface, most of the Map concrete classes extend AbstractMap class and implement required methods.

    AbstractMap类提供了Map接口的骨架实现,大多数Map具体类扩展了AbstractMap类并实现了所需的方法。

Java Map方法 (Java Map Methods)

Let’s have a look at some of the important Map methods.

让我们看一下一些重要的Map方法。

  1. int size(): returns the number of key-value mappings in this Map.

    int size() :返回此Map中的键值映射数。
  2. boolean isEmpty(): returns true if there are no mappings present, otherwise false.

    boolean isEmpty() :如果不存在映射,则返回true,否则返回false。
  3. boolean containsValue(Object value): returns true if there are at least one key mapped to the specified value, otherwise false.

    boolean containsValue(Object value) :如果至少有一个键映射到指定值,则返回true,否则返回false。
  4. V get(Object key): returns the value mapped to the given key, if no mapping found then returns null.

    V get(Object key) :返回映射到给定键的值,如果找不到映射,则返回null。
  5. V put(K key, V value): adds the mapping of key-value pair to the map. If there is already a value mapped to this key, then replace the value. This method returns the previous value associated with key, or null if there was no mapping for key.

    V put(K key,V value) :将键值对的映射添加到映射。 如果已经有一个映射到该键的值,则替换该值。 此方法返回与key关联的先前值;如果没有key映射,则返回null。
  6. V remove(Object key): Removes the mapping for a key from this map if it is present. Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

    V remove(Object key) :从此映射中删除键的映射(如果存在)。 返回此映射先前与该键关联的值;如果该映射不包含该键的映射,则返回null。
  7. void putAll(Map<? extends K, ? extends V> m): Copies all of the mappings from the specified map to this map.

    void putAll(Map <?扩展K,?扩展V> m) :将所有映射从指定映射复制到此映射。
  8. void clear(): removes all the mappings from the Map.

    void clear() :从地图中删除所有映射。
  9. Set<K> keySet(): returns the Set view of all the keys in the Map. This key set is backed by Map, so any modifications to Map will be reflected to the key set and vice versa.

    Set <K> keySet() :返回Map中所有键的Set视图。 此密钥集由Map支持,因此对Map的任何修改都会反映到密钥集上,反之亦然。
  10. Collection<V> values(): returns the collection view of all the values in the Map. This collection is backed by Map, so any change in Map will reflect to this values collection and vice versa.

    Collection <V> values() :返回Map中所有值的集合视图。 此集合由Map支持,因此Map中的任何更改都将反映到该值集合,反之亦然。
  11. Set<Map.Entry<K, V>> entrySet(): returns the Set view of the mappings in the Map. This Set is backed by Map, so any modifications in Map will be reflected in the entry set and vice versa.

    Set <Map.Entry <K,V >> entrySet() :返回Map中映射的Set视图。 此Set由Map支持,因此Map中的任何修改都将反映在条目集中,反之亦然。

There are few methods in Java Map introduced in Java 8.

Java 8中引入的Java Map中的方法很少。

  1. default V getOrDefault(Object key, V defaultValue): Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

    default V getOrDefault(Object key,V defaultValue) :返回指定键所映射到的值;如果此映射不包含该键的映射关系,则返回defaultValue。
  2. default void forEach(BiConsumer<? super K, ? super V> action): Performs the given action for each entry in this map.

    默认void forEach(BiConsumer <?super K,?super V> action) :对此映射中的每个条目执行给定的操作。
  3. default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function): Replaces each entry’s value with the result of invoking the given function on that entry.

    默认void replaceAll(BiFunction <?super K,?super V,?extends V> function) :用在该条目上调用给定函数的结果替换每个条目的值。
  4. default V putIfAbsent(K key, V value): If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

    默认V putIfAbsent(K键,V值) :如果指定键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则返回当前值。
  5. default boolean remove(Object key, Object value): Removes the entry for the specified key only if it is currently mapped to the specified value.

    default boolean remove(Object key,Object value) :仅当当前映射到指定值时,才删除指定键的条目。
  6. default boolean replace(K key, V oldValue, V newValue): Replaces the entry for the specified key only if currently mapped to the specified value.

    default boolean replace(K key,V oldValue,V newValue) :仅当当前映射到指定值时,才替换指定键的条目。
  7. default V replace(K key, V value): Replaces the entry for the specified key only if it is currently mapped to some value.

    默认V replace(K键,V值) :仅当当前映射到某个值时,才替换指定键的条目。
  8. default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction): If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.

    缺省的VulateIfAbsent(K键,Function <?超级K,?扩展V> mappingFunction) :如果指定的键尚未与某个值关联(或映射为null),则尝试使用给定的映射函数和除非为null,否则将其输入此地图。
  9. default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. If the function returns null, the mapping is removed.

    默认VcomputeIfPresent(K键,BiFunction <?super K,?super V,?extended V> remappingFunction) :如果指定键的值存在且不为空,则尝试计算给定键及其当前值的新映射映射值。 如果函数返回null,则删除映射。
  10. default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

    默认V计算(K键,BiFunction <?super K,?super V,?扩展V> remappingFunction) :尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为null)。
  11. default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction): If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

    默认V merge(K键,V值,BiFunction <?super V,?super V,?extended V> remappingFunction) :如果指定键尚未与值关联或与null关联,则将其与给定的non关联-null值。 否则,用给定的重映射函数的结果替换关联的值,如果结果为null,则将其删除。

You will notice that all the new methods added in the Java 8 Map interface are default methods with implementation. This is done to make sure no compilation error occurs for any classes implementing Map interface.

您会注意到,Java 8 Map接口中添加的所有新方法都是带有实现的默认方法。 这样做是为了确保任何实现Map接口的类都不会发生编译错误。

Java Map示例 (Java Map Example)

Let’s have a look at a simple program for Java Map example. We will use Map implementation class HashMap for our example program.

让我们看一下Java Map示例的简单程序。 我们将在示例程序中使用Map实现类HashMap。

package com.journaldev.examples;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapExample {

	public static void main(String[] args) {

		Map<String, String> data = new HashMap<>();

		data.put("A", "A"); // put example
		data.put("B", "B");
		data.put("C", "C");
		data.put("D", null); // null value
		data.put(null, "Z"); // null key

		String value = data.get("C"); // get example
		System.out.println("Key = C, Value = " + value);

		value = data.getOrDefault("E", "Default Value");
		System.out.println("Key = E, Value=" + value);

		boolean keyExists = data.containsKey(null);
		boolean valueExists = data.containsValue("Z");

		System.out.println("keyExists= " + keyExists + ", valueExists= " + valueExists);

		Set<Entry<String, String>> entrySet = data.entrySet();
		System.out.println(entrySet);

		System.out.println("data map size=" + data.size());

		Map<String, String> data1 = new HashMap<>();
		data1.putAll(data);
		System.out.println("data1 mappings= " + data1);

		String nullKeyValue = data1.remove(null);
		System.out.println("data1 null key value = " + nullKeyValue);
		System.out.println("data1 after removing null key = " + data1);

		Set<String> keySet = data.keySet();
		System.out.println("data map keys = " + keySet);

		Collection<String> values = data.values();
		System.out.println("data map values = " + values);

		data.clear();
		System.out.println("data map is empty =" + data.isEmpty());

	}

}

Output of above Map example program is;

以上Map示例程序的输出为;

Key = C, Value = C
Key = E, Value=Default Value
keyExists= true, valueExists= true
[null=Z, A=A, B=B, C=C, D=null]
data map size=5
data1 mappings= {null=Z, A=A, B=B, C=C, D=null}
data1 null key value = Z
data1 after removing null key = {A=A, B=B, C=C, D=null}
data map keys = [null, A, B, C, D]
data map values = [Z, A, B, C, null]
data map is empty =true

That’s all for a quick round up on Java Map interface. For Java Map example of new methods introduced in Java 8, please read Java HashMap.

这就是快速浏览Java Map界面的全部内容。 有关Java 8中引入的新方法的Java Map示例,请阅读Java HashMap

翻译自: https://www.journaldev.com/11641/java-map

java map中套map

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值