• HashMap<K,V>类


    目录

    所在包

    Type Parameters(参数类型)

    All Implemented Interfaces(所有实现接口)

    Direct Known Subclasses(直接已知子类)

    Constructor Summary(构造函数总结)

    Method Summary(方法总结)

    Constructor Detail(构造方法详细信息)

    Method Detail(方法详细信息)

      


    所在包:java.util.HashMap<K,V>

     

    Type Parameters(参数类型):
    K - the type of keys maintained by this map(映射维护键的类型)
    V - the type of mapped values(映射值类型)
    All Implemented Interfaces(所有实现接口):
    Serializable, Cloneable, Map<K,V>
    Direct Known Subclasses(直接已知子类):
    LinkedHashMap, PrinterStateReasons

     


    public class HashMap<K,V>

    extends AbstractMap<K,V>

    implements Map<K,V>,Cloneable,Serializable

    Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

    基于哈希表实现的映射接口。

    此实现提供所有可选的映射操作,并允许空值和空键。

    (HashMap类大致相当于Hashtable,只是它是不同步的,并且允许为空。)该类不保证映射的顺序;

    特别是,它不能保证订单在一段时间内保持不变。

    This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

    这个实现为基本操作(get和put)提供了稳定的时间性能,假设散列函数将元素适当地分散到各个bucket中。

    集合视图的迭代需要与HashMap实例的“容量”(桶的数量)及其大小(键值映射的数量)成比例的时间。

    因此,如果迭代性能很重要,那么不要将初始容量设置得太高(或负载因子太低)是非常重要的。

    An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

    HashMap实例有两个影响其性能的参数:初始容量和负载因子。
    容量是哈希表中的桶数,初始容量只是创建哈希表时的容量。
    负载因子是一个度量哈希表在其容量自动增加之前允许的满度的度量。
    当哈希表中的条目数超过负载因子和当前容量的乘积时,将对哈希表进行重新哈希(即重新构建内部数据结构),使哈希表的桶数大约是桶数的两倍。

    As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

    一般来说,默认的负载因子(.75)在时间和空间成本之间提供了很好的权衡。

    更高的值减少了空间开销,但增加了查找成本(反映在HashMap类的大多数操作中,包括get和put)。

    在设置map的初始容量时,应该考虑map中条目的期望数量及其负载因子,从而最小化rehash操作的数量。

    如果初始容量大于条目的最大数量除以负载因子,则不会发生重排操作。

    If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table. Note that using many keys with the same hashCode() is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.

    如果要将许多映射存储在HashMap实例中,那么使用足够大的容量创建映射将比根据需要执行自动散列来扩展表更有效地存储映射。

    注意,使用具有相同hashCode()的多个键肯定会降低任何散列表的性能。

    为了改善影响,当键是可比较的,这个类可以使用键之间的比较顺序来帮助打破联系。

    Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

    注意,这个实现不是同步的。

    如果多个线程同时访问一个散列映射,并且至少有一个线程从结构上修改了该映射,则必须在外部对其进行同步。

    (结构修改是指任何增加或删除一个或多个映射的操作;仅仅更改与一个实例已经包含的键相关联的值并不是结构修改。)

    这通常是通过在一些自然封装了映射的对象上进行同步来实现的。

    如果不存在这样的对象,则应该使用集合“包装”映射synchronizedMap方法。

    这最好在创建时完成,以防止意外的不同步访问映射:

       Map m = Collections.synchronizedMap(new HashMap(...));

    The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    这个类的所有“集合视图方法”返回的迭代器是快速失效的:如果在创建迭代器之后的任何时候,以任何方式(除了通过迭代器自己的删除方法)对映射进行结构修改,迭代器将抛出ConcurrentModificationException异常。
    因此,在面对并发修改时,迭代器会快速而干净地失败,而不是在将来某个不确定的时间冒任意的、不确定的行为的风险。

    Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

    注意,不能保证迭代器的快速故障行为,因为通常来说,在存在非同步并发修改的情况下,不可能做出任何严格的保证。

    故障快速迭代器在最大努力的基础上抛出ConcurrentModificationException。

    因此,编写一个依赖于这个异常的正确性的程序是错误的:迭代器的快速故障行为应该只用于检测bug。

    This class is a member of the Java Collections Framework.

    该类是Java集合框架的成员。

    Since从以下版本开始:
    1.2
    See Also另请参见:
    Object.hashCode() Collection,  Map,  TreeMap Hashtable,  Serialized Form

    Constructor Summary(构造函数总结

    Constructors 构造函数
    Constructor and Description构造函数和描述
    HashMap()
    Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
    使用默认初始容量(16)和默认负载因子(0.75)构造一个空HashMap。
    HashMap(int initialCapacity)
    Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
    构造一个具有指定初始容量和缺省负载因子(0.75)的空HashMap。
    HashMap(int initialCapacity, float loadFactor)
    Constructs an empty HashMap with the specified initial capacity and load factor.
    构造具有指定初始容量和负载因子的空HashMap。
    HashMap(Map<? extends K,? extends V> m)
    Constructs a new HashMap with the same mappings as the specified Map.
    使用与指定映射相同的映射构造新的HashMap。


    Method Summary
    (方法总结)

    Modifier and Type
    修饰符和类型
    Method and Description
    方法和描述
    void clear()
    Removes all of the mappings from this map.
    从该映射中删除所有映射。
    Object clone()
    Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
    返回此HashMap实例的浅拷贝:键和值本身没有被克隆。
    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).
    尝试为指定的键及其当前映射值计算映射(如果没有当前映射,则为null)。
    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.
    如果指定的键尚未与值关联(或映射为null),则尝试使用给定的映射函数计算其值并将其输入到此映射中,除非为null。
    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.
    如果指定键的值存在且非空,则尝试计算给定键及其当前映射值的新映射。
    boolean containsKey(Object key)
    Returns true if this map contains a mapping for the specified key.
    如果此映射包含指定键的映射,则返回true。
    boolean containsValue(Object value)
    Returns true if this map maps one or more keys to the specified value.
    如果此映射将一个或多个键映射到指定的值,则返回true。
    Set<Map.Entry<K,V>> entrySet()
    Returns a Set view of the mappings contained in this map.
    返回此映射中包含的映射的集合视图。
    void forEach(BiConsumer<? super K,? super V> action)
    Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
    为映射中的每个条目执行给定的操作,直到处理完所有条目或操作引发异常。
    V get(Object key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    返回指定键映射到的值,如果该映射不包含键的映射,则返回null。
    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.
    返回指定键映射到的值,如果此映射不包含键的映射,则返回defaultValue。
    boolean isEmpty()
    Returns true if this map contains no key-value mappings.
    如果此映射不包含键值映射,则返回true。
    Set<K> keySet()
    Returns a Set view of the keys contained in this map.
    返回此映射中包含的键的集合视图。
    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.
    如果指定的键尚未与值关联,或与null关联,则将其与给定的非空值关联。
    V put(K key, V value)
    Associates the specified value with the specified key in this map.
    将指定值与此映射中的指定键关联。
    void putAll(Map<? extends K,? extends V> m)
    Copies all of the mappings from the specified map to this map.
    将指定映射的所有映射复制到此映射。
    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.
    如果指定的键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则将返回当前值。
    V remove(Object key)
    Removes the mapping for the specified key from this map if present.
    如果存在,则从此映射中删除指定键的映射。
    boolean remove(Object key, Object value)
    Removes the entry for the specified key only if it is currently mapped to the specified value.
    仅当指定键当前映射到指定值时,删除指定键的项。
    V replace(K key, V value)
    Replaces the entry for the specified key only if it is currently mapped to some value.
    仅当指定键当前映射到某个值时,才替换该键的项。
    boolean replace(K key, V oldValue, V newValue)
    Replaces the entry for the specified key only if currently mapped to the specified value.
    仅当当前映射到指定值时,替换指定键的项。
    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 until all entries have been processed or the function throws an exception.
    将每个条目的值替换为对该条目调用给定函数的结果,直到处理完所有条目或该函数抛出异常。
    int size()
    Returns the number of key-value mappings in this map.
    返回此映射中的键值映射的数目。
    Collection<V> values()
    Returns a Collection view of the values contained in this map.
    返回此映射中包含的值的集合视图。


    Methods inherited from class java.util.AbstractMap

    从类 java.util.AbstractMap 继承的方法

    equals, hashCode, toString

    Methods inherited from class java.lang.Object

    从类 java.lang.Object 继承的方法

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Map

    从接口 java.util.Map 继承的方法

    equals, hashCode

    Constructor Detail (构造方法详细信息

    • HashMap

      public HashMap(int initialCapacity, float loadFactor)
      Constructs an empty HashMap with the specified initial capacity and load factor.
      构造一个带指定初始容量和加载因子的空 HashMap
    •  
      Parameters:
      initialCapacity - the initial capacity初始容量
      loadFactor - the load factor加载因子
      Throws:
      IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive 
      IllegalArgumentException - 如果初始容量为负或者加载因子为非正
    • HashMap

      public HashMap(int initialCapacity)
      Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
      构造一个带指定初始容量和默认加载因子 (0.75) 的空 HashMap
      Parameters:
      initialCapacity - the initial capacity初始容量.
      Throws:
      IllegalArgumentException - if the initial capacity is negative如果初始容量为负. 
    • HashMap

      public HashMap()
      Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
      构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap
    • HashMap

      public HashMap(Map<? extends K,? extends V> m)
      Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
      构造一个映射关系与指定 Map 相同的新 HashMap。所创建的 HashMap 具有默认加载因子 (0.75) 和足以容纳指定 Map 中映射关系的初始容量。 
      Parameters:
      m - the map whose mappings are to be placed in this map.
      m - 映射,其映射关系将存放在此映射中
      Throws:
      NullPointerException - if the specified map is null 如果指定的映射为 null


    Method Detail(方法详细信息)

    • size

      public int size()
      Returns the number of key-value mappings in this map.
      返回此映射中的键-值映射关系数。
      Specified by:
      size in interface Map<K,V>
      接口 Map<K,V> 中的 size
      Overrides:
      size in class AbstractMap<K,V>
      AbstractMap<K,V> 中的 size

      Returns:
      the number of key-value mappings in this map 
      此映射中的键-值映射关系数

     

    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      如果此映射不包含键-值映射关系,则返回 true。 
      Specified by:
      isEmpty in interface Map<K,V>
      接口 Map<K,V> 中的 isEmpty
      Overrides:
      isEmpty in class AbstractMap<K,V>
      AbstractMap<K,V> 中的 isEmpty
      Returns:
      true if this map contains no key-value mappings 
      如果此映射不包含键-值映射关系,则返回 true
    • get

      public V get(Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      返回指定键所映射的值;如果对于该键来说,此映射不包含任何映射关系,则返回 null。 

      More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

      更确切地讲,如果此映射包含一个满足 (key==null ? k==null : key.equals(k)) 的从 k 键到 v 值的映射关系,则此方法返回 v;否则返回 null。(最多只能有一个这样的映射关系。) 

      A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

      返回 null 值并不一定 表明该映射不包含该键的映射关系;也可能该映射将该键显示地映射为 null。可使用 containsKey 操作来区分这两种情况。 

       

      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Parameters:
      key - the key whose associated value is to be returned要返回其关联值的键
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key
      指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null
      See Also:
      put(Object, Object)

     

    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key.
      如果此映射包含对于指定键的映射关系,则返回 true。 
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Parameters:
      key - The key whose presence in this map is to be tested要测试其是否在此映射中存在的键
      Returns:
      true if this map contains a mapping for the specified key.  如果此映射包含对于指定键的映射关系,则返回 true
    • put

      public V put(K key,V value)
      Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
      在此映射中关联指定值与指定键。如果该映射以前包含了一个该键的映射关系,则替换旧值。
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Parameters:
      key - key with which the specified value is to be associated指定值将要关联的键
      value - value to be associated with the specified key指定键将要关联的值
      Returns:
      the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.) 
      key 关联的旧值;如果 key 没有任何映射关系,则返回 null。(返回 null 还可能表示该映射之前将 nullkey 关联。)

     

    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
      将指定映射的所有映射关系复制到此映射中,这些映射关系将替换此映射目前针对指定映射中所有键的所有映射关系。
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
      Parameters:
      m - mappings to be stored in this map要在此映射中存储的映射关系
      Throws:
      NullPointerException - if the specified map is null 如果指定的映射为 null
    • remove

      public V remove(Object key)
      Removes the mapping for the specified key from this map if present.
      从此映射中移除指定键的映射关系(如果存在)。
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      Parameters:
      key - key whose mapping is to be removed from the map其映射关系要从映射中移除的键
      Returns:
      the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.) 
      key 关联的旧值;如果 key 没有任何映射关系,则返回 null。(返回 null 还可能表示该映射之前将 nullkey 关联。)
    • clear

      public void clear()
      Removes all of the mappings from this map. The map will be empty after this call returns.
      从此映射中移除所有映射关系。此调用返回后,映射将为空。
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Returns true if this map maps one or more keys to the specified value.
      如果此映射将一个或多个键映射到指定值,则返回 true。 
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Parameters:
      value - value whose presence in this map is to be tested要测试其是否在此映射中存在的值
      Returns:
      true if this map maps one or more keys to the specified value
      如果此映射将一个或多个键映射到指定值,则返回 true  
    • keySet

      public Set<K> keySet()
      Returns a Set view of the keys contained in this map.
      The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
      If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
      The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
      It does not support the add or addAll operations.
      返回此映射中所包含的键的 Set 视图。
      该 set 受映射的支持,所以对映射的更改将反映在该 set 中,反之亦然。
      如果在对 set 进行迭代的同时修改了映射(通过迭代器自己的 remove 操作除外),则迭代结果是不确定的。
      该 set 支持元素的移除,通过 Iterator.removeSet.removeremoveAllretainAllclear 操作可从该映射中移除相应的映射关系。
      它不支持 addaddAll 操作。 
    • Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
      Returns:
      a set view of the keys contained in this map 
      此映射中包含的键的 set 视图
    • values

      public Collection<V> values()
      Returns a Collection view of the values contained in this map.
      The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
      If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
      It does not support the add or addAll operations.
      返回此映射所包含的值的 Collection 视图。
      该 collection 受映射的支持,所以对映射的更改将反映在该 collection 中,反之亦然。
      如果在对 collection 进行迭代的同时修改了映射(通过迭代器自己的 remove 操作除外),则迭代结果是不确定的。
      该 collection 支持元素的移除,通过 Iterator.removeCollection.removeremoveAllretainAllclear 操作可从该映射中移除相应的映射关系。
      它不支持 addaddAll 操作。
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
      Returns:
      a view of the values contained in this map 
      此映射中包含的值的 collection 视图
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns a Set view of the mappings contained in this map.
      The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
      If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined.
      The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
      It does not support the add or addAll operations.
      返回此映射所包含的映射关系的 Set 视图。
      该 set 受映射支持,所以对映射的更改将反映在此 set 中,反之亦然。
      如果在对 set 进行迭代的同时修改了映射(通过迭代器自己的 remove 操作,或者通过在该迭代器返回的映射项上执行 setValue 操作除外),则迭代结果是不确定的。该 set 支持元素的移除,通过 Iterator.removeSet.removeremoveAllretainAllclear 操作可从该映射中移除相应的映射关系。
      它不支持 addaddAll 操作。 
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
      Returns:
      a set view of the mappings contained in this map 
      此映射所包含的映射关系的 set 视图。
    • getOrDefault

      public V getOrDefault(Object key,V defaultValue)
      Description copied from interface: Map
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

      从接口复制的描述:Map映射
      返回指定键映射到的值,如果此映射不包含键的映射,则返回defaultValue。

      Specified by:
      getOrDefault in interface Map<K,V>
      Parameters:
      key - the key whose associated value is to be returned要返回其关联值的键
      defaultValue - the default mapping of the key键的默认映射
      Returns:
      the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key 
      指定键映射到的值,如果此映射不包含键的映射,则为defaultValue
    • putIfAbsent

      public V putIfAbsent(K key,V value)
      Description copied from interface: Map
      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.
      从接口复制的描述:Map映射
      如果指定的键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则将返回当前值。
    • Specified by:
      putIfAbsent in interface Map<K,V>
      Parameters:
      key - key with which the specified value is to be associated要与指定值关联的键
      value - value to be associated with the specified key要与指定键关联的值
      Returns:
      the previous value associated with the specified key, or null if there was no mapping for the key.
      (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.) 

      与指定键关联的前一个值,如果没有该键的映射,则为null。
      (如果实现支持null值,则null返回也可以指示以前将null与键关联的映射。)

    • remove

      public boolean remove(Object key,Object value)
      Description copied from interface: Map
      Removes the entry for the specified key only if it is currently mapped to the specified value.

      从接口复制的描述:Map映射
      仅当指定键当前映射到指定值时,删除指定键的项。

      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key - key with which the specified value is associated与指定值关联的键
      value - value expected to be associated with the specified key预期与指定键关联的值
      Returns:
      true if the value was removed 如果值被移除,则为真
    • replace

      public boolean replace(K key,V oldValue,V newValue)
      Description copied from interface: Map
      Replaces the entry for the specified key only if currently mapped to the specified value.

      从接口复制的描述:Map映射
      仅当当前映射到指定值时,替换指定键的项。

      Specified by:
      replace in interface Map<K,V>
      Parameters:
      key - key with which the specified value is associated与指定值关联的键
      oldValue - value expected to be associated with the specified key预期与指定键关联的值
      newValue - value to be associated with the specified key要与指定键关联的值
      Returns:
      true if the value was replaced 如果值被替换,则为真
    • replace

      public V replace(K key,V value)
      Description copied from interface: Map
      Replaces the entry for the specified key only if it is currently mapped to some value.

      从接口复制的描述:Map映射
      仅当指定键当前映射到某个值时,才替换该键的项。

      Specified by:
      replace in interface Map<K,V>
      Parameters:
      key - key with which the specified value is associated与指定值关联的键
      value - value to be associated with the specified key将指定值与指定键关联的键
      Returns:
      the previous value associated with the specified key, or null if there was no mapping for the key.
      (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.) 
      与指定键关联的前一个值,如果没有该键的映射,则为null。
      (如果实现支持null值,则null返回也可以指示以前将null与键关联的映射。)
    • computeIfAbsent

      public V computeIfAbsent(K key,Function<? super K,? extends V> mappingFunction)
      Description copied from interface: Map
      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.
      从接口复制的描述:Map映射
      如果指定的键尚未与值关联(或映射为null),则尝试使用给定的映射函数计算其值并将其输入到此映射中,除非为null。

      If the function returns null no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:
      如果函数返回null,则不记录映射。
      如果函数本身抛出一个(未检查的)异常,则会重新抛出异常,并且不会记录任何映射。
      最常见的用法是构造一个新对象作为初始映射值或默记结果,如:

       map.computeIfAbsent(key, k -> new Value(f(k)));

      Or to implement a multi-value map, Map<K,Collection<V>>, supporting multiple values per key:
      或者实现一个多值映射,map <K,Collection<V>>,每个键支持多个值:

       map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
      Specified by:
      computeIfAbsent in interface Map<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      要与指定值关联的键
      mappingFunction - the function to compute a value
      用来计算一个值的函数
      Returns:
      the current (existing or computed) value associated with the specified key, or null if the computed value is null 
      与指定键关联的当前(现有的或已计算的)值,如果计算值为空,则为空
    • computeIfPresent

      public V computeIfPresent(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
      Description copied from interface: Map
      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.
      从接口复制的描述:Map映射
      如果指定键的值存在且非空,则尝试计算给定键及其当前映射值的新映射。

      If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
      如果函数返回null,则映射将被删除。
      如果函数本身抛出一个(未检查的)异常,则会重新抛出异常,并且当前映射保持不变。

      Specified by:
      computeIfPresent in interface Map<K,V>
      Parameters:
      key - key with which the specified value is to be associated要与指定值关联的键
      remappingFunction - the function to compute a value用来计算一个值的函数
      Returns:
      the new value associated with the specified key, or null if none
      与指定键关联的新值,如果没有则为空
    • compute

      public V compute(K key,
                       BiFunction<? super K,? super V,? extends V> remappingFunction)
      Description copied from interface: Map
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping:
      从接口复制的描述:Map映射
      尝试为指定的键及其当前映射值计算映射(如果没有当前映射,则为null)。
      例如,创建或附加一个字符串msg到一个值映射:
       map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
      (Method merge() is often simpler to use for such purposes.)
      (方法merge()通常更易于用于此类目的。)

      If the function returns null, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
      如果函数返回null,则映射将被删除(如果最初没有映射,则映射将保持为空)。

      如果函数本身抛出一个(未检查的)异常,则会重新抛出异常,并且当前映射保持不变。

      Specified by:
      compute in interface Map<K,V>
      Parameters:
      key - key with which the specified value is to be associated要与指定值关联的键
      remappingFunction - the function to compute a value用来计算一个值的函数
      Returns:
      the new value associated with the specified key, or null if none  
      与指定键关联的新值,如果没有则为空
    • merge

      public V merge(K key,V value,BiFunction<? super V,? super V,? extends V> remappingFunction)
      Description copied from interface: Map
      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.
      This method may be of use when combining multiple mapped values for a key.
      For example, to either create or append a String msg to a value mapping:
      从接口复制的描述:Map映射
      如果指定的键尚未与值关联,或与null关联,则将其与给定的非空值关联。
      否则,将关联值替换为给定的重新映射函数的结果,如果结果为空,则删除关联值。
      此方法可用于组合一个键的多个映射值。
      例如,创建或附加一个字符串msg到一个值映射:
       map.merge(key, msg, String::concat)

      If the function returns null the mapping is removed.
      If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
      如果函数返回null,则删除映射。
      如果函数本身抛出一个(未检查的)异常,则会重新抛出异常,并且当前映射保持不变。

      Specified by:
      merge in interface Map<K,V>
      Parameters:
      key - key with which the resulting value is to be associated要将结果值与之关联的键
      value - the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key将与与键关联的现有值合并的非空值,或者,如果没有与键关联的现有值或空值,则与键关联
      remappingFunction - the function to recompute a value if present如果存在,重新计算值的函数
      Returns:
      the new value associated with the specified key, or null if no value is associated with the key 
      与指定键关联的新值,如果没有与键关联的值,则为null
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Description copied from interface: Map
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
      Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.)
      Exceptions thrown by the action are relayed to the caller.
      从接口复制的描述:Map映射
      为映射中的每个条目执行给定的操作,直到处理完所有条目或操作引发异常。
      除非实现类另行指定,否则操作将按照条目集迭代的顺序执行(如果指定了迭代顺序)。
      操作引发的异常将被转发给调用者。
      Specified by:
      forEach in interface Map<K,V>
      Parameters:
      action - The action to be performed for each entry 要为每个条目执行的操作
    • replaceAll

      public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
      Description copied from interface: Map
      Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.
      从接口复制的描述:Map映射
      将每个条目的值替换为对该条目调用给定函数的结果,直到处理完所有条目或该函数抛出异常。
      函数抛出的异常将被转发给调用者。
      Specified by:
      replaceAll in interface Map<K,V>
      Parameters:
      function - the function to apply to each entry 应用于每个条目的函数
    • clone

      public Object clone()
      Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
      返回此HashMap实例的浅拷贝:键和值本身没有被克隆。
      Overrides:
      clone in class AbstractMap<K,V>
      Returns:
      a shallow copy of this map这张地图的浅复制
      See Also:
      Cloneable

  • 相关阅读:
    texturePacker黄色文件夹和蓝色文件夹
    自定义相机下使用clippingNode注意事项
    cocos2dx 3.3多相机下_transformUpdated bug
    一定不要在头文件中using namespace XXX
    cocos2dx 3.3将坐标由父空间转化到局部空间
    cocos2dx 3.3 getParentToNodeTransform bug
    cocos2dx 3.x designResolutionSize须主动设置
    mysql操作查询结果case when then用法举例
    java List.subList方法中的超级大陷阱
    Linux下修改Mysql的用户(root)的密码
  • 原文地址:https://www.cnblogs.com/LinQingYang/p/12545623.html
Copyright © 2020-2023  润新知